我想从下面的xml中提取2个节点insu和/insu之间的数据
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<Qres xmlns="http://www.test.com/h/xsd">
<PMR xmlns:xsd="http://www.test.com/h/xsd">
<ID xmlns="http://example.services/Results.xsd" xmlns:ns1="urn:Group/20160505/Ons" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">1159</ID>
<ref xmlns="http://example.services/Results.xsd" xmlns:ns1="urn:Group/20160505/Ons" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">12345</ref>
<SingleQres xmlns="http://example.services/Results.xsd" xmlns:ns1="urn:Group/20160505/AddOns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Qres>
<ref>12345</ref>
<SC>ok</SC>
<Sche>
<csc>Car</csc>
<Insu>
<Entry>
<Key>ok</Key>
<Value>hello</Value>
<MetData>
<Key>test</Key>
<Value>test</Value>
</MetData>
</Entry>
</Insu>
</Sche>
</Qres>
</SingleQres>
</PMR>
</Qres>
我编写了以下代码并能够获得它。年份是测试步骤的名称,其结果在XML
之上def c=context.expand('${Years#Response#//*:Qres//*:Sche//*:Insu/*}')
log.info c
输出低于
<res:Entry xmlns:res="http://example.services/Results.xsd">
<res:Key>ok</res:Key>
<res:Value>hello</res:Value>
<res:MetData>
<res:Key>test</res:Key>
<res:Value>test</res:Value>
</res:MetData>
</res:Entry>
问题: - 它附加了名称空间。
我希望XML为,没有,只有下面的节点应该像原始XML
中的那样。 <Entry>
<Key>ok</Key>
<Value>hello</Value>
<MetData>
<Key>test</Key>
<Value>test</Value
如果使用上述命令进行一些调整,我可以在没有名称空间的情况下获得节点,因为我想在下一步中使用这些值没有命名空间前缀
您可以使用XmlSlurper
无需命名空间,而XmlUtil
可以序列化,如下所示:
def xml = new XmlSlurper(false, false).parseText(context.expand('${Years#Response}'))
log.info groovy.xml.XmlUtil.serialize(xml.'**'.find{it.name() == 'Insu'}.children())
您可以快速尝试相同的在线 demo
编辑:基于OP评论。
XmlNodePrinter
无需XML标记声明即可获取它。
def xml = new XmlParser(false, false).parseText(context.expand('${Years#Response}'))
def entry = xml.'**'.find{it.name() == 'Entry'}
def sw = new StringWriter()
new XmlNodePrinter(new PrintWriter(sw)).print(entry)
log.info sw.toString()
这是相同的演示。
@rao提供的解决方案也有效。非常感谢详细说明。
我正在使用以下解决方案,我认为该解决方案更简单地理解,并且适用于上面的示例xml
def groovyUtils=new com.eviware.soapui.support.GroovyUtils(context)
def res = groovyUtils.getXmlHolder("Years#Response")
def xmlData = res.getXmlObject()
String str=xmlData.toString()
def a=str.split("<Insu>")[1].split("</Insu>")
log.info a[0]
@rao的答案也可以正常工作