Ask a Question

Can't get node values

New Contributor

Can't get node values



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:


<AddressLine>Fanshawe Street</AddressLine>

<AddressLine>Federal Street</AddressLine>



And here's my code:



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

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


But all the output is null!


Please help me, I have spent hours on this 😞




try this

import groovy.xml.XmlUtil
def groovyUtils = new context )

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

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


Thanks T3D, this was my solution:



def holder = new 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);


Showing results for 
Search instead for 
Did you mean: