I'm trying to search these occurrences in xml file:
<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/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
There are useful free tools for seeing the results of XPath queries without needing to write java upfront:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.