Not able to loop over XML tags in groovy properly

三世轮回 提交于 2019-12-02 13:19:52

问题


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


回答1:


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()
}


来源:https://stackoverflow.com/questions/43328952/not-able-to-loop-over-xml-tags-in-groovy-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!