document上的xpath字符串定位元素

document上的xpath字符串定位元素

本文介绍了Java:如何通过org.w3c.dom.document上的xpath字符串定位元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过指定的org.w3c.dom.document上的xpath字符串快速定位元素/元素?似乎没有 FindElementsByXpath()方法。例如

How do you quickly locate element/elements via xpath string on a given org.w3c.dom.document? there seems to be no FindElementsByXpath() method. For example

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

我发现,当有很多相同元素的元素时,我会循环遍历所有子节点级别。任何建议?

I found that recursively iterating through all the child node levels to be quite slow when there are lot of elements of same name. Any suggestions?

我不能使用任何解析器或库,只能使用w3c dom文档。

I cannot use any parser or library, must work with w3c dom document only.

推荐答案

尝试这样:

//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.getDocumentElement(), XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); ++i) {
    Element e = (Element) nodes.item(i);
}

使用以下 page.html 文件:

<html>
  <head>
  </head>
  <body>
  <p>
    <div></div>
    <div></div>
    <div><a>link</a></div>
  </p>
  </body>
</html>

这篇关于Java:如何通过org.w3c.dom.document上的xpath字符串定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:21