无法正确循环 groovy 中的 XML 标签

Not able to loop over XML tags in groovy properly

我能够通过 soapUI 发送一个网络请求,它以 XML 格式给我数据作为响应。我想在数据库 tables 中插入 xml 标签的值。

这是我试过的:

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


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

'response' 变量的内容:

<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>

'xml' 的内容:

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

请注意 'RSXS4_1true' 被添加到 'xml' 中,因此我无法使用

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

遍历 xml 标签。

更准确地说,我想获取 'John' 和 'Max' 并将它们插入一些 table。 欢迎任何帮助

因为您的数据在 CDATA 块中,所以它被视为字符串(然后需要按原样重新解析 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()
}