Forum Discussion

ncandy's avatar
ncandy
New Contributor
8 years ago

Can't get node values

Hi,

 

I need to parse a list of XML objects. I'm having a lot of difficulty actually getting the values out of the XML nodes.

Here is sample XML data:

  

<GetAddressHintsResponse>
<GetAddressHintsResult>
<AddressHint>
<Town>Auckland</Town>
<AddressLine>Fanshawe Street</AddressLine>
<Postcode>1010</Postcode>
<StreetType>Street</StreetType>
</AddressHint>

<AddressHint>
<Town>Auckland</Town>
<AddressLine>Federal Street</AddressLine>
<Postcode>1010</Postcode>
<StreetType>Street</StreetType>
</AddressHint>
</GetAddressHintsResult>
</GetAddressHintsResponse>

 

 

And here's my code:

 

import com.eviware.soapui.support.XmlHolder

def holder = new XmlHolder( messageExchange.responseContentAsXml )
holder.namespaces["ns1"] = "http://iag.co.nz/Wizit"
def nodes = holder.getDomNodes( "//ns1:GetAddressHintsResponse[1]/ns1:GetAddressHintsResult[1]/ns1:AddressHint" )

for (node in nodes)
	log.info node.getAttributes().getNamedItem("Town")

 

But all the output is null!

 

Please help me, I have spent hours on this :(

2 Replies

  •  

    ncandy

    try this

    import groovy.xml.XmlUtil
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

    def holder = groovyUtils.getXmlHolder( "Soap1#Request" ) //replace "Soap1" with your request name

    //for( item in holder.getNodeValues( "//tem:Add/tem:intA" ))
    //log.info "Item : [$item]"

     log.info holder.getNodeValue( "//tem:Add/tem:intA" )

    • ncandy's avatar
      ncandy
      New Contributor

      Thanks T3D, this was my solution:

       

       

      def holder = new com.eviware.soapui.support.XmlHolder( messageExchange.responseContentAsXml );
      holder.namespaces.put("ns1", "http://mynamespace" );
      
      // get the count of how many address hints there are
      def count = holder.getDomNodes("//ns1:GetAddressHintsResponse/ns1:GetAddressHintsResult/ns1:AddressHint").length;
      
      // make a new array and populate it with the data we need to check for hint equality
      AddressHint[] hints = new AddressHint[count];
      for (def i = 1; i <= count; i++) {
      	def add = holder.getNodeValue("//ns1:GetAddressHintsResponse[1]/ns1:GetAddressHintsResult[1]/ns1:AddressHint["+i+"]/ns1:AddressLine");
      	def twn = holder.getNodeValue("//ns1:GetAddressHintsResponse[1]/ns1:GetAddressHintsResult[1]/ns1:AddressHint["+i+"]/ns1:Town");
      	def pst = holder.getNodeValue("//ns1:GetAddressHintsResponse[1]/ns1:GetAddressHintsResult[1]/ns1:AddressHint["+i+"]/ns1:Postcode");
      	def sub = holder.getNodeValue("//ns1:GetAddressHintsResponse[1]/ns1:GetAddressHintsResult[1]/ns1:AddressHint["+i+"]/ns1:Suburb");
      	hints[i-1] = new AddressHint(add, twn, pst, sub);
      }