Java:如何通过org.w3c.dom.document上的xpath字符串定位元素

Java:如何通过org.w3c.dom.document上的xpath字符串定位元素,第1张

Java:如何通过org.w3c.dom.document上的xpath字符串定位元素

试试这个:

//obtain document somehow, doesn't matter howdocumentBuilder b = documentBuilderFactory.newInstance().newdocumentBuilder();org.w3c.dom.document doc = b.parse(new FileInputStream("page.html"));//evaluate XPath against document itselfXPath 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);}

带有以下

page.html
文件:

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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5499580.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存