无法正确循环Groovy中的XML标签 [英] Not able to loop over XML tags in groovy properly

查看:83
本文介绍了无法正确循环Groovy中的XML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我所尝试过的:

pre $ 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-instancexmlns:xsd =http://www.w3.org/2001/XMLSchemaxmlns: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>
< /行>
< / 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>
< Column0> Max< / Column0>
< /行>
< / rowset> RSXS4_1true

请注意'RSXS4_1true'正在追加'xml '因此,我无法使用

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

循环xml标签。



更准确地说,我想获取'John'和'Max'并将它们插入某个表中。
任何帮助都是最受欢迎的

解决方案

因为您的数据位于CDATA块中,所以它被视为String然后需要重新解析,因为它是XML)

  //解析xml 
def xml = new XmlSlurper()。parseText(response)

//获取cdata文本
def cdata = xml.Body.executeXMLQueryResult.return.rowset.text()

//重新解析它
def innerXml = new XmlSlurper()。parseText(cdata)

//然后迭代行
innerXml.Row.each {row - >
println row.Column0.text()
}


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

解决方案

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

这篇关于无法正确循环Groovy中的XML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆