试试这个:
//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>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)