我已经使用POI从Excel中读取,现在我试图捕获响应并将其写回Excel,这里是代码
<AtomicWeight>1.00797</AtomicWeight>
我想从响应中将1.00797写入excel中。但是请求中不存在此标记。有什么方法可以实现吗?
//响应消息
<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">
<soap:Body>
<GetAtomicWeightResponse xmlns="http://www.webserviceX.NET">
<GetAtomicWeightResult><![CDATA[<NewDataSet>
<Table>
// want to capture this value(1.00797) and write it in excel
<AtomicWeight>1.00797</AtomicWeight>
</Table>
</NewDataSet>]]></GetAtomicWeightResult>
</GetAtomicWeightResponse>
</soap:Body>
</soap:Envelope>
在这里输入代码
在这里输入代码
答案 0 :(得分:0)
访问AtomicWeight
元素很棘手,因为它包含在CDATA中。 XmlSlurper无法解析它。但你可以通过...来实现它。
GetAtomicWeightResult
元素并转换为
串GetAtomicWeightResult
String-ified元素。AtomicWeight
元素。import groovy.util.XmlSlurper
def text = '''<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">
<soap:Body>
<GetAtomicWeightResponse xmlns="http://www.webserviceX.NET">
<GetAtomicWeightResult><![CDATA[<NewDataSet>
<Table>
// want to capture this value(1.00797) and write it in excel
<AtomicWeight>1.00797</AtomicWeight>
</Table>
</NewDataSet>]]></GetAtomicWeightResult>
</GetAtomicWeightResponse>
</soap:Body>
</soap:Envelope>'''
def xml = new XmlSlurper().parseText(text)
def cdata = new XmlSlurper().parseText(xml.Body.GetAtomicWeightResponse.GetAtomicWeightResult.toString())
assert cdata.Table.AtomicWeight == 1.00797