使用java在xml中搜索结束排除
我正在尝试在xml文件中搜索这些事件:使用java在xml中搜索结束排除,java,xml,xpath,Java,Xml,Xpath,我正在尝试在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"/>
<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());
XPath=XPathFactory.newInstance().newXPath();
String path=“//xml//corpus//build//s//graph//nonterminals//nt”;
XPathExpression expr=xpath.compile(路径);
System.out.println(“Query1:+path”);
对象结果=expr.evaluate(文档,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
System.out.println(“节点数:+nodes.getLength());
对于(inti=0;i
这个问题正确吗?
还有别的办法吗?
如何删除节点结果?如果这是XML的整个结构,并且
是根节点,那么XPath应该是:
/xml/corpus/build/s/graph/nonterminals/nt
有一些有用的免费工具可以查看XPath查询的结果,而无需预先编写java:
>/P>>在