【问题标题】:XML Parser GroovyXML 解析器 Groovy
【发布时间】:2014-09-18 09:22:22
【问题描述】:

我在 SOAPUI 中使用 Groovy Script 来实现几个模拟服务。我正在尝试通过 groovy 脚本修改 XML 文件,但它不起作用。请帮帮我

我想更改此文件中<STATUS> 的值

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns5:PCH1CRTO_REC xmlns:ns2="http://www.csc.smart/bo/schemas/Error" xmlns:ns3="http://www.csc.smart/bo/schemas/PCH1CRTI" xmlns:ns4="http://www.csc.smart/msp/schemas/MSPContext" xmlns:ns5="http://www.csc.smart/bo/schemas/PCH1CRTO" xmlns:ns6="http://www.csc.smart/bo/schemas/PCH1ENQI" xmlns:ns7="http://www.csc.smart/bo/schemas/PCH1ENQO">
         <STATUS>0</STATUS>

我在 SOAPUI 中的 groovy 脚本

filePath = groovyUtils.projectPath + "\\Response-Client-Details\\Response-Policy-List\\Response-Policy-Details\\" + fileArray[i] + ".xml"
def root = xmlParser.parse(filePath)

接下来我要做什么?我尝试了很多方法,但它不起作用,甚至得到&lt;STATUS&gt; 的值。谢谢

【问题讨论】:

  • 它最终奏效了吗?
  • 还没有,我不能写xml文件
  • 我修好了,因为昨天加班太晚了。我有解决方案!你是最好的合作伙伴,非常感谢@mitomed

标签: groovy soapui


【解决方案1】:

所以你在根变量中有 xml,现在你需要使用 XPath 访问你的 STATUS 元素,并更新它

应该是这样的(你也应该导入 groovy.xml.XmlUtil)

def soapenv =  new groovy.xml.Namespace("http://schemas.xmlsoap.org/soap/envelope/")
def ns5 = new groovy.xml.Namespace("http://www.csc.smart/bo/schemas/PCH1CRTO")

root[soapenv.Body][ns5.PCH1CRTO_REC].[STATUS][0].value = 1

为了保存它,试试这个

def outputFile = groovyUtils.projectPath + "\\Response-Client-Details\\Response-Policy-List\\Response-Policy-Details\\" + fileArray[i] + ".xml"
def f = XmlUtil.serialize(root)
def of = new File(outputFile)
of.write(f, "UTF-8")

【讨论】:

  • 为什么只有 xmlns:ns5="csc.smart/bo/schemas/PCH1CRTO"。你能解释一下为什么吗?谢谢!
  • 顺便说一句,它不起作用,但有异常:没有这样的属性 STATUS :(
  • 你能粘贴整个 XMl 和几个属性吗?我会尝试自己测试一下
  • 我的 XML 文件太长了,所以将它作为评论发布,但它很简单。和我的帖子一样。请帮帮我,我花了整个下午的时间。
  • 并且 Status 没有嵌套在任何其他定义的命名空间下,对吧? ns5以下
【解决方案2】:

我找到了最好的答案,试试这个:

log.info root[soapenv.Body][ns5.PCH1CRTO_REC].STATUS[0].text() 

不是

log.info root[soapenv.Body][ns5.PCH1CRTO_REC][STATUS][0].text()

然后保存

new XmlNodePrinter(new PrintWriter(new FileWriter(filePath))).print(root)

无论如何,非常感谢@mitomed,你帮了我一半!

【讨论】:

  • 酷,我不确定,只是想给你我最好的提示,因为前几天我自己也在努力解决这个问题stackoverflow.com/questions/25804789/wcf-xsdate-using-groovy
  • 但是我遇到了另一个麻烦,我无法更新&lt;STATUS&gt; 的新值? .value 似乎不起作用。你有什么想法@mitomed
  • 当您记录它时,您会得到 0?
  • log.info ('VALUE:' + root[soapenv.Body][ns5.PCH1CRTO_REC].STATUS[0].text())root[soapenv.Body][ns5.PCH1CRTO_REC].STATUS[0].value = 1这样,但是不行!
  • 我的意思是,您现在访问它的方式(状态之前的点)被正确记录为 0?。你得到 VALUE:0?
猜你喜欢
  • 2023-04-05
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
相关资源
最近更新 更多