我有以下肥皂回应:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetHTMLResponse xmlns="http://www.webserviceX.NET">
<GetHTMLResult>
TEST
</GetHTMLResult>
</GetHTMLResponse>
</soap:Body>
</soap:Envelope>
现在我想要一个方法来传递给我这个xml:
<root>
<GetHTMLResponse xmlns="http://www.webserviceX.NET">
<GetHTMLResult>
TEST
</GetHTMLResult>
</GetHTMLResponse>
</root>
所以Soap信封和身体应该被移除,Soap Body中的所有东西都应该在标签根目录下。我该怎么做?
答案 0 :(得分:1)
所以你可以这样做:
import groovy.xml.*
def xml = '''<?xml version="1.0" encoding="utf-8"?>
|<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
| <soap:Body>
| <GetHTMLResponse xmlns="http://www.webserviceX.NET">
| <GetHTMLResult>
| TEST
| </GetHTMLResult>
| </GetHTMLResponse>
| </soap:Body>
|</soap:Envelope>'''.stripMargin()
def result = XmlUtil.serialize(new StreamingMarkupBuilder().bind {
root {
mkp.yield new XmlSlurper(false, false).parseText(xml).'soap:Body'.GetHTMLResponse
}
})
println result
输出:
<?xml version="1.0" encoding="UTF-8"?><root>
<GetHTMLResponse xmlns="http://www.webserviceX.NET">
<GetHTMLResult>
TEST
</GetHTMLResult>
</GetHTMLResponse>
</root>