ImranRazaKhan ImranRazaKhan - 2 months ago 10
Groovy Question

Parse Same level Node in SOAP UI and Groovy

I have below XML and want to parse to get values

<member>
<name>accumulatorInformation</name>
<value>
<array>
<data>
<value>
<struct>
<member>
<name>accumulatorEndDate</name>
<value>
<dateTime.iso8601>20161004T12:00:00+0000</dateTime.iso8601>
</value>
</member>
<member>
<name>accumulatorID</name>
<value>
<i4>1</i4>
</value>
</member>
<member>
<name>accumulatorStartDate</name>
<value>
<dateTime.iso8601>20160904T12:00:00+0000</dateTime.iso8601>
</value>
</member>
<member>
<name>accumulatorValue</name>
<value>
<i4>0</i4>
</value>
</member>
</struct>
</value>
<value>
<struct>
<member>
<name>accumulatorEndDate</name>
<value>
<dateTime.iso8601>20161017T12:00:00+0000</dateTime.iso8601>
</value>
</member>
<member>
<name>accumulatorID</name>
<value>
<i4>2</i4>
</value>
</member>
<member>
<name>accumulatorStartDate</name>
<value>
<dateTime.iso8601>20160917T12:00:00+0000</dateTime.iso8601>
</value>
</member>
<member>
<name>accumulatorValue</name>
<value>
<i4>0</i4>
</value>
</member>
</struct>
</value>
</data>
</array>
</value>
</member>


I tried to parse and only want accumulatorID and accumulatorValue.

Following is my code in SOAPUI as groovy step

def responseHolder = groovyUtils.getXmlHolder("Get#Response")
for( struct in responseHolder.getNodeValues( "//member[name='accumulatorInformation']/value/array/data/value/struct/*" ))
log.info "struct : [$struct]"
log.info "accumulatorID : " + struct.getNodeValues( "//member[name='accumulatorID']/value/descendant::*" )


It shows output like below and second line showing error

Mon Sep 26 16:54:01 GST 2016:INFO:struct : [ ]
Mon Sep 26 16:54:01 GST 2016:INFO:struct : [ ]
Mon Sep 26 16:54:01 GST 2016:INFO:struct : [ ]

Answer

Your xml is not well formatted but this

import groovy.xml.XmlUtil

content =   """    <value>
         <struct>
           <member>
              <name>accumulatorEndDate</name>
              <value><dateTime.iso8601>20161004T12:00:00+0000</dateTime.iso8601></value>
           </member>
           <member>
             <name>accumulatorID</name>
             <value><i4>1</i4></value>
           </member>
           <member>
             <name>accumulatorStartDate</name>
                 <value><dateTime.iso8601>20160904T12:00:00+0000</dateTime.iso8601></value>
           </member>
           <member>
             <name>accumulatorValue</name>
             <value><i4>0</i4></value>
           </member>
      </struct>
      </value>

      """
      def p =  new XmlSlurper().parseText(content)



      // def v= p.value.struct.member[0].name.text()
       assert "accumulatorEndDate"==p.struct.member[0].name.text().toString()

        println (p.struct.member[1].value.i4.text())
        println (p.struct.member[3].value.i4.text())

will print the required values

1
0

you can also do

  p.struct.member.each{
    println "${it.name.text()} "
    println "${it.value.text()} "


   } 

to print these

accumulatorEndDate 
20161004T12:00:00+0000 
accumulatorID 
1 
accumulatorStartDate 
20160904T12:00:00+0000 
accumulatorValue 
0