解码SOAP信封时遇到问题。下面是我的XML
<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:tns="http://c.com/partner/">
<env:Header>c
<tns:MessageId env:mustUnderstand="true">3</tns:MessageId>
</env:Header>
<env:Body>
<GetForkliftPositionResponse xmlns="http://www.c.com">
<ForkliftId>PC006</ForkliftId>
</GetForkliftPositionResponse>
</env:Body>
</env:Envelope>我使用以下代码来解码body,但它总是返回到名称空间tns:MessageID,而不是env:body。我还想将XMLStreamReader转换为字符串以解决调试问题,这是可能的吗?
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty("javax.xml.stream.isCoalescing", true); // decode entities into one string
StringReader reader = new StringReader(Message);
String SoapBody = "";
XMLStreamReader xsr = xif.createXMLStreamReader( reader );
xsr.nextTag(); // Advance to header tag
xsr.nextTag(); // advance to envelope
xsr.nextTag(); // advance to body发布于 2013-04-04 22:22:34
最初,xsr在文档事件(即XML声明)之前指向,nextTag()前进到下一个标记,而不是下一个同级元素
xsr.nextTag(); // Advance to opening envelope tag
xsr.nextTag(); // advance to opening header tag
xsr.nextTag(); // advance to opening MessageId如果你想跳过正文,一个更好的习惯用法应该是
boolean foundBody = false;
while(!foundBody && xsr.hasNext()) {
if(xsr.next() == XMLStreamConstants.START_ELEMENT &&
"http://www.w3.org/2003/05/soap-envelope".equals(xsr.getNamespaceURI()) &&
"Body".equals(xsr.getLocalName())) {
foundBody = true;
}
}
// if foundBody == true, then xsr is now pointing to the opening Body tag.
// if foundBody == false, then we ran out of document before finding a Body
if(foundBody) {
// advance to the next tag - this will either be the opening tag of the
// element inside the body, if there is one, or the closing Body tag if
// there isn't
if(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
// now pointing at the opening tag of GetForkliftPositionResponse
} else {
// now pointing at </env:Body> - body was empty
}
}发布于 2013-04-04 22:18:54
在xsr.nextTag()读取QName之后,您可以从中获得标记名和前缀
QName qname = xsr.getName();
String pref = qname.getPrefix();
String name = qname.getLocalPart();https://stackoverflow.com/questions/15813467
复制相似问题