1

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

Rao
  • 20,781
  • 11
  • 57
  • 77
Gaurav Khurana
  • 3,423
  • 2
  • 29
  • 38

2 Answers2

1

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.

Rao
  • 20,781
  • 11
  • 57
  • 77
  • Thanks @Rao.. 2 Concerns 1) is coming in output which is not expected 2) Could you please explain 1 line for both the steps i mean in first step you got all values of all nodes and in the second you used that xml variable and serialize.. Could you please explain in short what actually happened in these 2 steps.. for understanding purpose – Gaurav Khurana Feb 08 '18 at 03:09
  • @Gauravkhurana, what is your use case as you wanted to extract part of xml instead of node values? – Rao Feb 08 '18 at 03:53
  • We get some information from A XML and we have to pass the same information inside B XML. So manually we copy paste. For automation we need groovy to copy that particular information to XML B – Gaurav Khurana Feb 08 '18 at 06:03
  • @Gauravkhurana, how do you include the extracted one into next xml? – Rao Feb 08 '18 at 06:04
  • @Gauravkhurana, [Let us chat over your queries](https://chat.stackoverflow.com/rooms/164726/discussion-between-rao-and-gauravkhurana) – Rao Feb 08 '18 at 06:19
  • @Gauravkhurana, appreciate if you can accept the above solution as answered and upvote as you mentioned in the chat that it worked. – Rao Feb 08 '18 at 09:20
  • Yes i accept that it works and i would like to wait and see few more answers before marking this as a solution. As i agreed on chat i have upvoted – Gaurav Khurana Feb 08 '18 at 10:47
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164739/discussion-between-rao-and-gaurav-khurana). – Rao Feb 08 '18 at 10:54
  • @Gauravkhurana, you still looking for more while the above address your issue as it is not marked answered though you said it worked on chat? – Rao Feb 09 '18 at 10:22
1

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

Gaurav Khurana
  • 3,423
  • 2
  • 29
  • 38