无法正确循环 XML 标记



我能够通过soapUI发送一个Web请求,它为我提供了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 标记 。

更准确地说,我想获取"约翰"和"马克斯"并将它们插入某个表中。欢迎任何帮助

由于您的数据位于 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()
}

最新更新