Forum Discussion

sumitmishra's avatar
sumitmishra
Occasional Contributor
15 years ago

xml parsing with groovy

I am extracting a row of data from database, and one of the field value is an escaped xml body. I am trying to parse this xml using groovy to get a particular field. her's how my script looks:

import com.eviware.soapui.support.XmlHolder;

//Parse table row returned
def holder = new XmlHolder(messageExchange.responseContentAsXml);

//Store the field value that has xml in 'xml_data'
def xml_data = holder.getNodeValue("//Results//ResultSet//Row[1]//XML_DATA");

//Start parsing of xml_data
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
def holder1 = groovyUtils.getXmlHolder(xml_data);
holder1.namespaces["ns"] = "http://www.xxx.com/xx/yy;

//Pull out the field 'OrderNumber' from the nested xml
def on = holder1.getNodeValue("//ns:XData/OrderNumber");

log.info("xml_data: " + xml_data);
log.info("orderNumber: " + on);


This results in following:
Thu Jul 15 15:11:14 EDT 2010:INFO:xml_data&colon; <?xml version="1.0" encoding="UTF-8"?>{XML Body output}
Thu Jul 15 15:11:14 EDT 2010:INFO:orderNumber:

it does not reutrn the field value to me. Am i approaching this correctly?

3 Replies

  • sumitmishra's avatar
    sumitmishra
    Occasional Contributor
    I got it working. The change was:
    def on = holder1.getNodeValue("//ns:XData/ns:OrderNumber");
  • Hi Sumit,

    I am just doing the xml parsing part in the script assertion out of the response xml obtained. I am doing the same way as you showed here, but getting the order number as empty even I followed the same way of

    def on = holder1.getNodeValue("//ns:XData/ns:OrderNumber");

    //Start parsing of xml_data
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
    def holder1 = groovyUtils.getXmlHolder(xml_data);
    holder1.namespaces["ns"] = "http://www.xxx.com/xx/yy;

    //Pull out the field 'OrderNumber' from the nested xml
    def on = holder1.getNodeValue("//ns:XData/OrderNumber");

    log.info("xml_data&colon; " + xml_data);
    log.info("orderNumber: " + on);

    Could you please help me with this error?
  • registering the name space was a problem. Thats the reason it was failing.

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def responseHolder = groovyUtils.getXmlHolder( messageExchange.responseContent )

    responseHolder.namespaces['ns2']='http://www.xyz.com/schemas/xyz/klm';
    responseHolder.namespaces['ns1']='http://www.xyz.com/schemas/xyz/def';
    responseHolder.namespaces['ns4']='http://www.xyz.com/schemas/xyz/abc';
    responseHolder.namespaces['ns3']='http://www.xyz.com/schemas/xyz/pqr';

    once we have the name spaces we should register them before we get the node value of the response