日期:2014-05-20  浏览次数:20585 次

新手向高手求点代码, 使用DOM4J 解析XML 的
XML是一个SOAP, 内容如下:
XML code

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body><ACIC_MessageBrokerService xmlns="http://www.ACIC.MessageBrokerService.com/">
<ServiceID>0428000001</ServiceID>
<obj>
  <anyType xsi:type="xsd:string">par1</anyType>
  <anyType xsi:type="xsd:int">0</anyType>
  <anyType xsi:type="xsd:boolean">true</anyType>
</obj></ACIC_MessageBrokerService>
</soap:Body>
</soap:Envelope>



请高手帮忙帮我取出 ServiceID 的值放进一个String变量, 和 obj 中的值, 放进一个Object[] 中, 万分感谢....

------解决方案--------------------
对了,如果你喜欢用XPath的话,可能要让你失望了:

Element node = doc.selectSingleNode("//*[local-name()='ServiceID' and namespace-uri()='http://www.ACIC.MessageBrokerService.com/']");
------解决方案--------------------
不会啊,我试验的挺好的,你是不是变量重名了?

Java code

        Element obj = root.element("Body").element("ACIC_MessageBrokerService").element("obj");
        List<Element> types = obj.elements("anyType");
        for (Element elem : types) {
            System.out.println(elem.getName() + ": " + elem.getText());
        }