I'm trying to do the following.
我试着做下面的事情。
OMElement soapEnvelope = new StAXOMBuilder(soapEnvelopXMLFilePath).getDocumentElement();
OMElement firstElement = soapEnvelope.getFirstElement().getFirstElement();
Then i iterate through the child elements of the firstElement like this.
然后像这样迭代firstElement的子元素。
Iterator<OMElement> items = firstElement.getChildren();
while (items.hasNext()) {
OMElement element = items.next();
// ....do some processing here...
}
But when i try to execute this following class cast exception occurs.
但是当我尝试执行下面的类强制转换时,会发生异常。
java.lang.ClassCastException: org.apache.axiom.om.impl.llom.OMTextImpl cannot be cast to org.apache.axiom.om.OMElement
The error occurs when items.next()
is assigned to element OMElement
object.
当项.next()被分配给元素OMElement对象时发生错误。
Any idea why i'm getting this exception? I can't figure out any mismatch.
知道我为什么会有这个例外吗?我不知道有什么不匹配。
These are the contents of my sample xml file.
这些是我的示例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>
<ns:HOSTS>
<ns:HOST NAME="node3">
<ns:METRIC NAME="metric1" VALUE="236"/>
<ns:METRIC NAME="metric3" VALUE="159"/>
<ns:METRIC NAME="metric2" VALUE="478"/>
</ns:HOST>
</ns:HOSTS>
</ns:MONITOR>
</soapenv:Body>
Thanks.
谢谢。
4
Use getChildElements
instead of getChildren
.
使用getChildElements而不是getChildren。
2
Not every node is an element. You should cast children to OMNode and check, if they are elements, Whitespaces count as text nodes
不是每个节点都是元素。您应该将子节点强制转换为OMNode,并检查它们是否为元素,是否为文本节点
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/06/03/d8c40b824161c22483acd61c35a42735.html。