我正在尝试解析文件中的 SOAP 响应。 这是out.xml
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<response xmlns="http://tempuri.org/">
<result>
<config>...</config>
<config>...</config>
<config>...</config>
</result>
</response>
</soap:Body>
</soap:Envelope>
这是使用 jdom 的代码:
SAXBuilder builder = new SAXBuilder();
try {
Document document = builder.build( new File("out.xml"));
Element root = document.getRootElement();
Namespace ns = Namespace.getNamespace("http://tempuri.org/");
List r = root.getChildren("config", ns);
System.out.println(r.size());
}
为什么输出0?
最佳答案
JDOM 的 getChildren
方法记录如下(强调我的):
This returns a List of all the child elements nested directly (one level deep) within this element, as Element objects.
查看原文here .
对 getRootElement
的调用会将您带到 soap:Envelope
,它没有任何 config
子节点。
要解决此问题,您可以:
- 多次调用
getChildren
以浏览soap:Body
、response
和result
元素 - 调用
getDescendants
来获取一个遍历整个层次结构而不仅仅是一级的迭代器
关于java - 使用 JDOM 解析文件中的 SOAP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13982505/