簡體   English   中英

搜索結束用Java在xml中排除

[英]Search end exclude in xml with java

我正在嘗試在xml文件中搜索這些事件:

<xml>  
<corpus>  
<body>  
<nonterminals>  
<graph>  
<s>  
<nt id="s9_509" cat="fcl"></nt>  

<nt id="s9_501" cat="pp">  
                <edge label="H" idref="s9_1"/>  
                <edge label="DP" idref="s9_502"/>  
                <edge label="STA" idref="s9_509"/>  
                <edge label="P" idref="s9_19"/>  
                <edge label="S" idref="s9_510"/>  
                <edge label="PU" idref="s9_25"/>  
            </nt>  
</nonterminals>  
</graph>  
</s>  
</body>  
</corpus>  
</xml>  

我的代碼是:

XPath xpath = XPathFactory.newInstance().newXPath();    
String path = "//xml//corpus//build//s//graph//nonterminals//nt";  
XPathExpression expr = xpath.compile(path);    
System.out.println("Query1: "+path);  
Object result = expr.evaluate(document, XPathConstants.NODESET);    
NodeList nodes = (NodeList) result;    
System.out.println("Number of nodes: "+nodes.getLength());  
for (int i = 0; i < nodes.getLength(); i++) {    
    System.out.println(nodes.item(i).getAttributes().getNamedItem("id").getNodeValue());  

這個查詢正確嗎? 存在另一種嘗試方法嗎? 如何刪除節點結果?

假設這是XML的整個結構,而<xml>是您的根節點,那么我想說您的XPath應該是:

/xml/corpus/build/s/graph/nonterminals/nt

如果要匹配每個 nt節點而不管它在文檔中出現的位置如何,可以使用:

//nt

有一些有用的免費工具,無需查看Java就可以查看XPath查詢的結果:

http://xml-copy-editor.sourceforge.net/

http://www.xpathtester.com/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM