我正在尝试执行以下操作。

OMElement soapEnvelope = new StAXOMBuilder(soapEnvelopXMLFilePath).getDocumentElement();
OMElement firstElement = soapEnvelope.getFirstElement().getFirstElement();


然后,我像这样遍历firstElement的子元素。

Iterator<OMElement> items = firstElement.getChildren();
while (items.hasNext()) {
    OMElement element = items.next();
    // ....do some processing here...
}


但是,当我尝试执行此以下类时,将发生强制转换异常。

java.lang.ClassCastException: org.apache.axiom.om.impl.llom.OMTextImpl cannot be cast to org.apache.axiom.om.OMElement


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>
                <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>




谢谢。

最佳答案

使用getChildElements代替getChildren

10-04 22:16
查看更多