joe
joe

Reputation: 173

Not able to loop over XML tags in groovy properly

I was able to send a webrequest by soapUI which gives me data in XML format as response .I want to insert the value of xml tag in database tables.

This is what I have tried :

def response = context.expand('${Request1#Response}')


def xml =  new XmlSlurper().parseText(response)

Content of 'response' variable :

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:sawsoap="urn://oracle.bi.webservices/v7">
<soap:Body>
<sawsoap:executeXMLQueryResult>
         <sawsoap:return xsi:type="sawsoap:QueryResults">
<sawsoap:rowset xsi:type="xsd:string"><![CDATA[<rowset xmlns="urn:schemas-microsoft-com:xml-analysis:rowset">

<Row>
<Column0>John</Column0>

</Row>

<Row>

    <Column0>Max</Column0>
</Row>
</rowset>]]></sawsoap:rowset>
<sawsoap:queryID xsi:type="xsd:string">RSXS4_1</sawsoap:queryID>
<sawsoap:finished xsi:type="xsd:boolean">true</sawsoap:finished>
</sawsoap:return>
 </sawsoap:executeXMLQueryResult>
</soap:Body>
</soap:Envelope>

Content of 'xml' :

<rowset xmlns="urn:schemas-microsoft-com:xml-analysis:rowset">
<Row>
<Column0>John </Column0>
</Row>
<Row>
<Column0>Max </Column0>
</Row>
</rowset>RSXS4_1true

Please note 'RSXS4_1true' is getting appended in 'xml' because of which I am not able to use

xml.Row.each{ Row-> log.info "${Row.Column0.text()}"  }

to loop through the xml tags .

To be more precise I want to fetch 'John' and 'Max' and insert them in some table. Any help is most welcome

Upvotes: 1

Views: 233

Answers (1)

tim_yates
tim_yates

Reputation: 171084

Because your data is in a CDATA block, it is treated as a String (and then needs to be re-parsed as it is XML)

// Parse the xml
def xml =  new XmlSlurper().parseText(response)

// Get the cdata text
def cdata = xml.Body.executeXMLQueryResult.return.rowset.text()

// Re-parse it
def innerXml = new XmlSlurper().parseText(cdata)

// Then iterate the rows
innerXml.Row.each { row ->
    println row.Column0.text()
}

Upvotes: 1

Related Questions