无法正确循环遍历groovy中的XML标记

时间:2017-04-10 16:58:05

标签: database groovy soapui

我能够通过soapUI发送webrequest,它以XML格式提供数据作为响应。我想在数据库表中插入xml标记的值。

这就是我的尝试:

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


def xml =  new XmlSlurper().parseText(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'并将它们插入某些表中。 欢迎任何帮助

1 个答案:

答案 0 :(得分:1)

因为您的数据位于CDATA块中,所以它被视为String(然后需要重新解析,因为它是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()
}