2015-09-02 74 views
1

我有一些問題,當我使用if語句for環內的XML文檔中刪除一些節點,因爲我不明白爲什麼我的計數器成爲0把if語句後:Java的XPath的「if」語句

public static void main(String[] args) throws Exception { 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    Document document = dbf.newDocumentBuilder().parse(new File("input3.xml")); 


    XPathFactory xpf = XPathFactory.newInstance(); 
    XPath xpath = xpf.newXPath(); 

    XPathExpression expres = xpath.compile("//TNMS/TPaths/TPath[Topology/LayerSet/NonTerminatedLayers/Layer='RS64']"); 
    NodeList PathsCount = (NodeList) expres.evaluate(document, XPathConstants.NODESET); 

    System.out.println("Inicia el Borrado:"); 

    for (int i = 0; i < PathsCount.getLength(); i++) 
    { 
     System.out.println(i); 

     XPathExpression exprueba = xpath.compile("/TNMS/TPaths/TPath[1]/Topology/LayerSet/TerminatedLayers"); 
     NodeList Count = (NodeList) exprueba.evaluate(document, XPathConstants.NODESET); 
     System.out.println("Hay " + Count.getLength()); 
     int countvar = Count.getLength(); 

     XPathExpression expression = xpath.compile("//TNMS/TPaths/TPath[Topology/LayerSet/NonTerminatedLayers/Layer='RS64']"); 
     Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE); 
     b13Node.getParentNode().removeChild(b13Node); 
    } 

我看到在輸出下一:

Inicia el Borrado: 
0 

Hay 0 

1 

Hay 1 

2 

Hay 1 

3 

Hay 0 

<?xml version="1.0" encoding="UTF-8" standalone="no"?><TNMS> 
    <TPaths> 

    </TPaths> 
</TNMS>BUILD SUCCESSFUL (total time: 0 seconds) 

但是,當我同意if聲明:

public static void main(String[] args) throws Exception { 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     Document document = dbf.newDocumentBuilder().parse(new File("input3.xml")); 


     XPathFactory xpf = XPathFactory.newInstance(); 
     XPath xpath = xpf.newXPath(); 

     XPathExpression expres = xpath.compile("//TNMS/TPaths/TPath[Topology/LayerSet/NonTerminatedLayers/Layer='RS64']"); 
     NodeList PathsCount = (NodeList) expres.evaluate(document, XPathConstants.NODESET); 

     System.out.println("Inicia el Borrado:"); 

     for (int i = 0; i < PathsCount.getLength(); i++) 
     { 
      System.out.println(i); 

      XPathExpression exprueba = xpath.compile("/TNMS/TPaths/TPath[1]/Topology/LayerSet/TerminatedLayers"); 
      NodeList Count = (NodeList) exprueba.evaluate(document, XPathConstants.NODESET); 
      System.out.println("Hay " + Count.getLength()); 
      int countvar = Count.getLength(); 

      if (countvar == 1) { 
       XPathExpression expression = xpath.compile("//TNMS/TPaths/TPath[Topology/LayerSet/NonTerminatedLayers/Layer='RS64']"); 
       Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE); 
       b13Node.getParentNode().removeChild(b13Node); 
      } 
     } 

輸出是下一個:

Inicia el Borrado: 
0 

Hay 0 

1 

Hay 0 

2 

Hay 0 

3 

Hay 0 

和擦除失敗。

任何想法?

+0

您的預期成果是什麼? 'if'語句中的代碼只會在'countvar'爲1時執行。但是什麼改變'countvar'?它有沒有改變? – alondono

+1

請考慮發佈您的XML來源。如果問題出現在XPath表達式中,那麼沒有辦法在沒有看到XML源代碼的情況下對其進行測試。 – sideshowbarker

+0

非常感謝您的回答,我沒有發佈XML,因爲它太分享了。但它可以用一些新的方法。 –

回答

0

你需要評估你的for循環中的xpath表達式和你迭代的節點。在你的代碼中,你會一直引用TPath[1],這會導致各種奇怪的問題。

評估您的xpath表達式相對於您存儲在您的NodeList中的當前TPaths節點。

for (int i = 0; i < PathsCount.getLength(); i++) 
{ 
    Node tPathsChild = PathsCount.item(i); 
    System.out.println(i); 

    // get the number of TerminatedLayers elements. 
    XPathExpression exprueba = xpath.compile("Topology/LayerSet/TerminatedLayers"); 
    NodeList Count = (NodeList) exprueba.evaluate(tPathsChild, XPathConstants.NODESET); 
    System.out.println("Hay " + Count.getLength()); 
    int countvar = Count.getLength(); 

    // remove this child if there are only 1 of them. 
    if (countvar == 1) { 
     tPathsChild.getParentNode().removeChild(tPathsChild); 
    } 
} 

FYI:的Java命名約定是camelCase。對於Java程序員來說,當它們不是這種格式時,可能很難閱讀。

+0

嘿,非常感謝,我很熟悉Java和數據編程方面的新內容,因爲我正在研究硬件,所以我對此不以爲然。但它對我來說很有用,它會學習你的解釋和你寫的駱駝。 –

0

,你應該對它進行調試,但我認爲它是因爲Count.getLength()返回0,你如果statment只有當它返回1

這就是可能是因爲當你運行程序第一次刪除節點,你沒有被執行恢復文件時,如果你的文件是空的話,你可以試着使用

+0

好吧,我試試吧.. –