Java: how to find an element via xpath string on org.w3c.dom.document

How do you quickly find the item (s) through the xpath line in the given org.w3c.dom.document file? there seems to be no FindElementsByXpath() method. for example

 /html/body/p/div[3]/a 

I found that recursively iterating over all child levels of a node will be quite slow when there are many elements with the same name. Any suggestions?

I can’t use any parser or library, it should work only with the w3c dom document.

+51
java dom xpath
Jun 30 2018-11-17T00:
source share
1 answer

Try this:

 //obtain Document somehow, doesn't matter how DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); org.w3c.dom.Document doc = b.parse(new FileInputStream("page.html")); //Evaluate XPath against Document itself XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList)xPath.evaluate("/html/body/p/div[3]/a", doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); ++i) { Element e = (Element) nodes.item(i); } 

With the following page.html file:

 <html> <head> </head> <body> <p> <div></div> <div></div> <div><a>link</a></div> </p> </body> </html> 
+86
Jun 30 '11 at 17:58
source share



All Articles