简体   繁体   English

搜索结束用Java在xml中排除

[英]Search end exclude in xml with java

I'm trying to search these occurrences in xml file: 我正在尝试在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>  

And my code is: 我的代码是:

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());  

This query its correct? 这个查询正确吗? Exists another way to try it? 存在另一种尝试方法吗? How do I delete the Node findings? 如何删除节点结果?

Providing that this is the entire structure of your XML and <xml> is your root node, I would say that your XPath should be: 假设这是XML的整个结构,而<xml>是您的根节点,那么我想说您的XPath应该是:

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

If you want to match every nt node regardless of where it appears in the document, you could use: 如果要匹配每个 nt节点而不管它在文档中出现的位置如何,可以使用:

//nt

There are useful free tools for seeing the results of XPath queries without needing to write java upfront: 有一些有用的免费工具,无需查看Java就可以查看XPath查询的结果:

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

http://www.xpathtester.com/ http://www.xpathtester.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM