我正在尝试执行以下操作。
OMElement soapEnvelope = new StAXOMBuilder(soapEnvelopXMLFilePath).getDocumentElement();
OMElement firstElement = soapEnvelope.getFirstElement().getFirstElement();
Run Code Online (Sandbox Code Playgroud)
然后我像这样遍历 firstElement 的子元素。
Iterator<OMElement> items = firstElement.getChildren();
while (items.hasNext()) {
OMElement element = items.next();
// ....do some processing here...
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试执行以下类强制转换时发生异常。
java.lang.ClassCastException: org.apache.axiom.om.impl.llom.OMTextImpl cannot be cast to org.apache.axiom.om.OMElement
Run Code Online (Sandbox Code Playgroud)
items.next()分配给元素OMElement对象时发生错误。
知道为什么我会收到此异常吗?我想不出任何不匹配。
这些是我的示例 xml 文件的内容。
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<ns:MONITOR xmlns:ns="http://try.example.com">
<ns:HOSTS>
<ns:HOST NAME="node1">
<ns:METRIC NAME="metric1" VALUE="1123"/>
<ns:METRIC NAME="metric3" VALUE="456"/>
<ns:METRIC NAME="metric2" VALUE="789"/>
</ns:HOST>
<ns:HOST NAME="node2">
<ns:METRIC NAME="metric1" VALUE="147"/>
<ns:METRIC NAME="metric3" VALUE="258"/>
<ns:METRIC NAME="metric2" VALUE="369"/>
</ns:HOST>
</ns:HOSTS> …Run Code Online (Sandbox Code Playgroud)