Interview Questions

XML nodes -- Iterate nodes


(Continued from previous question...)

XML nodes -- Iterate nodes

A common and equally straight forward task, use the GroovyUtils class to create an XmlHolder, which you can use for a variety of XML related activities:

1.// create groovyUtils and XmlHolder for response of Request 1 request
2.def groovyUtils = new context )
3.def holder = groovyUtils.getXmlHolder( "Request 1#Response" )
5.// loop item nodes in response message
6.for( item in holder.getNodeValues( "//item" ))
7. "Item : [$item]"

If the desired content is namespace qualified (very likely for SOAP responses), you need to define the namespace first. Continuoing from above:

1.// define namespace
2.holder.namespaces["ns"] = ""
4.// loop item nodes in response message
5.for( item in holder.getNodeValues( "//ns:item" ))
6. "Item : [$item]"

(Continued on next question...)

Other Interview Questions