如何在没有命名空间的情况下从 SoapUI Groovy 中的 Soap Response 中提取 XML 节点?
How to extract XML nodes from Soap Response in SoapUI Groovy without namespaces?
我想从下面的 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>
我写了下面的代码并且能够得到它。 Years是结果在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()
这是相同的demo。
@Rao提供的解决方案也worked.Thanks很多,解释的很详细
我正在使用下面的解决方案,我觉得它更容易理解并且适用于上面的示例 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 的回答也很好
我想从下面的 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>
我写了下面的代码并且能够得到它。 Years是结果在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()
这是相同的demo。
@Rao提供的解决方案也worked.Thanks很多,解释的很详细
我正在使用下面的解决方案,我觉得它更容易理解并且适用于上面的示例 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 的回答也很好