0

我正在尝试从文件中解析 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?

4

1 回答 1

1

JDOM 的getChildren方法是这样记录的(强调我的):

这将返回直接嵌套在该元素内(一层深)的所有子元素的列表,作为 Element 对象。

在这里查看原文。

您对 的调用getRootElement将您置于soap:Envelope没有任何子config节点的 。

要解决此问题,您可以:

  1. getChildren多次调用以浏览soap:Body,responseresult元素
  2. 调用getDescendants以获取遍历整个层次结构而不仅仅是一个级别的迭代
于 2012-12-21T00:19:16.473 回答