2014-10-31 72 views
0

我有一個xml文件,我想讀取xml的所有子節點的值。我的xml是如何在java中使用xpathh讀取xml的所有節點?

<branches> 
     <branch-area name="abc"> 
      <branch> 
       <branch-name> xyz Street</branch-name> 
       <branchID>5689742</branchID> 
       <branchAddress>xyz address</branchAddress> 
       <atm>true</atm> 
       <branch>true</branch> 
       <tab title="Contact"> 
     <![CDATA[<table> 
     <tr> 
      <td class="head">Branch</td> 
      <td>bandra Street</td> 
     </tr> 
     <tr> 
      <td class="head">Address</td> 
      <td>bandra Street</td> 
     </tr> 
     <tr> 
      <td class="head">Sort Code</td> 
      <td>215863</td> 
     </tr> 
     </table>]]> 
    </tab> 
      </branch> 
     </branch-area> 
     <branch-area name="def 11"> 
      <branch> 
       <branch-name>pqr</branch-name> 
       <branchID>123456</branchID> 
       <branchAddress>pqr address </branchAddress> 
       <atm>true</atm> 
       <branch>true</branch> 
       <tab title="Contact"> 
     <![CDATA[<table> 
     <tr> 
      <td class="head">Branch</td> 
      <td>bandra Street</td> 
     </tr> 
     <tr> 
      <td class="head">Address</td> 
      <td>bandra Street</td> 
     </tr> 
     <tr> 
      <td class="head">Sort Code</td> 
      <td>215863</td> 
     </tr> 
     </table>]]> 
      </tab> 
      </branch> 
     </branch-area> 
     <branch-area name="ghi 14"> 
      <branch> 
       <branch-name>jkl</branch-name> 
       <branchID>589674</branchID> 
       <branchAddress>jkl address</branchAddress> 
       <atm>true</atm> 
       <branch>true</branch> 
<tab title="Contact"> 
     <![CDATA[<table> 
     <tr> 
      <td class="head">Branch</td> 
      <td>bandra Street</td> 
     </tr> 
     <tr> 
      <td class="head">Address</td> 
      <td>bandra Street</td> 
     </tr> 
     <tr> 
      <td class="head">Sort Code</td> 
      <td>215863</td> 
     </tr> 
     </table>]]> 
    </tab> 
      </branch> 
     </branch-area> 
    </branches> 

並且我正在使用此xpath expreesion獲取特定分支。

String xpathExpression = "/branches/branch-area[name='abc']/branch"; 

它返回我即

<branch> 
        <branch-name> xyz Street</branch-name> 
        <branchID>5689742</branchID> 
        <branchAddress>xyz address</branchAddress> 
        <atm>true</atm> 
        <branch>true</branch> 
<tab title="Contact"> 
     <![CDATA[<table> 
     <tr> 
      <td class="head">Branch</td> 
      <td>bandra Street</td> 
     </tr> 
     <tr> 
      <td class="head">Address</td> 
      <td>bandra Street</td> 
     </tr> 
     <tr> 
      <td class="head">Sort Code</td> 
      <td>215863</td> 
     </tr> 
     </table>]]> 
    </tab> 
       </branch> 

特定的分支,但我想它的孩子像分支名稱branchID等方面的價值,做我需要使用另一個XPath表達式爲每子節點還是有另一種方式呢?如果是,請指導我

回答

1

你可以...

使用像/branches/branch-area[@name='abc']/branch/*的路徑將返回你NodeList包含與branch所有子節點。

XPath xpath = XPathFactory.newInstance().newXPath(); 
XPathExpression branchExp = xpath.compile("/branches/branch-area[@name='abc']/branch/*"); 
NodeList branchNodes = (NodeList) branchExp.evaluate(dom, XPathConstants.NODESET); 
System.out.println(branchNodes.getLength()); 
for (int index = 0; index < branchNodes.getLength(); index++) { 
    Node node = branchNodes.item(index); 
    System.out.println(node.getTextContent()); 
} 

,其輸出類似...

5 
xyz Street 
5689742 
xyz address 
true 

你可以...

搜索每個單獨的子節點,使用branch節點作爲搜索的父節點.. 。

XPath xpath = XPathFactory.newInstance().newXPath(); 
XPathExpression branchExp = xpath.compile("/branches/branch-area[@name='abc']/branch"); 
Node branchNode = (Node) branchExp.evaluate(dom, XPathConstants.NODE); 

XPathExpression nameExp = xpath.compile("branch-name/text()"); 
String name = (String) nameExp.evaluate(branchNode, XPathConstants.STRING); 
System.out.println("Name = " + name); 

其中輸出類似...

Name = xyz Street 

更新...

所以,CDATA不被XPATH處理,相反,你需要得到tab節點的文本,它解析爲Document和在它運行的XPath ,例如...

try { 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder db = dbf.newDocumentBuilder(); 
    Document dom = db.parse(new File("Test.xml")); 
    XPath xpath = XPathFactory.newInstance().newXPath(); 

    // Find the "thing" node... 
    XPathExpression thingExpr = xpath.compile("/branches/branch-area/branch/tab"); 
    Node things = (Node) thingExpr.evaluate(dom, XPathConstants.NODE); 
    String table = things.getTextContent(); 

    ByteArrayInputStream bais = new ByteArrayInputStream(table.getBytes()); 
    Document tblDom = db.parse(bais); 
    XPathExpression tableExp = xpath.compile("/table/tr[td[text()='Sort Code']]/td[not(@*)]"); 
    NodeList nodes = (NodeList) tableExp.evaluate(tblDom, XPathConstants.NODESET); 
    System.out.println(nodes.getLength()); 
    for (int index = 0; index < nodes.getLength(); index++) { 
     Node node = nodes.item(index); 
     System.out.println(node.getTextContent()); 
    } 
} catch (Exception exp) { 
    exp.printStackTrace(); 
} 
+0

嗨,我只是更新我的xml,如果我想要一個表內的值我怎麼能得到它們? – user2142786 2014-10-31 07:37:29

+0

你需要創建適當的路徑,但過程是一樣的 – MadProgrammer 2014-10-31 07:45:33

+0

好的謝謝隊友:) – user2142786 2014-10-31 07:48:35