我已经使用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>
AtomicWeight
元素很棘手,因为它包装在CDATA中。 XmlSlurper无法解析。但是你可以通过...]GetAtomicWeightResult
元素并转换为字符串GetAtomicWeightResult
字符串化的元素。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