簡體   English   中英

如何在沒有命名空間的 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

如果使用上面的命令進行一些調整,我可以獲得沒有命名空間的節點,因為我想在沒有命名空間前綴的后續步驟中使用這些值,這將是很棒的

您可以使用沒有命名空間的XmlSlurperXmlUtil進行序列化,如下所示:

def xml = new XmlSlurper(false, false).parseText(context.expand('${Years#Response}'))
log.info groovy.xml.XmlUtil.serialize(xml.'**'.find{it.name() == 'Insu'}.children())

您可以快速嘗試相同的在線演示

編輯:基於 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 的回答也很好

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM