This is a question about xml parsing.

Asked 2 years ago, Updated 2 years ago, 69 views

<a>
    <b>123213</b>
    <c>
        <c1>a</c1>
        <c2>b</c2>
    </c>
    <d>123123</d>
</a>

I'm currently trying to parse nodes in an xml file using Java's xpath (javax.xml.xpath.XPath)

Can't I get a node or a nodelist so that I can tour b,c,d in the lower part of a node?

xml

2022-09-22 21:40

1 Answers

XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList)xpath.evaluate("//a/*", document, XPathConstant.NODESET);
for(int i=0;i<nl.getLength();i++) {
  Element e = (Element)nl.getItem(i);
}

I think we can do it the same way as above.

In the case of XPathConstant.NODESET, you can convert the return type to NodeList and use it.

The above //a/* Xpath expression means subnode of all a.


2022-09-22 21:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.