2012-12-21 22 views
0

我試圖解析來自文件的SOAP響應。 這是out.xml使用JDOM解析來自文件的SOAP響應

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

回答

1

JDOM的getChildren方法記錄在案,因爲這(重點煤礦):

這將返回直接嵌套這個元素中的所有子元素(一級深),如Element對象的名單。

查看原始here

您致電getRootElement會讓您看到soap:Envelope,它沒有任何config子節點。

要解決這個問題,你可以:

  1. 呼叫getChildren多次通過soap:Bodyresponseresult元素導航
  2. 呼叫getDescendants得到一個迭代器,確實遍歷整個層次而不只是一個級別
+0

謝謝。其實我直接尋找特定元素的簡單方法。但它有幫助。 – miqbal