简体   繁体   中英

How to extract XML nodes from Soap Response in SoapUI Groovy without namespaces?

I want to extract the data between 2 nodes Insu and /Insu from the below XML without namespaces in output

<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>

I wrote the below code and able to get it. Years is the name of the test step whose result is above XML

def c=context.expand('${Years#Response#//*:Qres//*:Sche//*:Insu/*}')
log.info c

The output is below

<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>

Problem :- It has namespace appended to it.

I wanted XML to be without , only nodes should be there like below as it was there in original XML

   <Entry>
   <Key>ok</Key>
   <Value>hello</Value>
   <MetData>
   <Key>test</Key>
   <Value>test</Value

It would be great if using the above command with some tweak i can get the nodes without namespaces as i want to use those values in next steps without namespace prefixed

You can use XmlSlurper without namespace, and XmlUtil to serialize as shown below:

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

You can quickly try the same online demo

EDIT: based on OP comment.

XmlNodePrinter be used to get it without xml markup declaration.

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()

Here is the demo for the same.

The solution Provided by @Rao also worked.Thanks a lot for the detailed explanation.

I am using the below solution which i feel is a little simpler to understand and works for the above example 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's answer is also working fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM