我使用JTidy来解析网页数据。我的问题如下:
可以在先前检索到的节点上调用XPath.evalate方法吗?
我会解释得更好。通常,您使用xmlPath.evaluate(pattern,document,XPathConstants.NODE)方法调用来检索与xpath表达式匹配的节点列表。
一旦检索到节点或nodeList,如何从以前检索的节点开始执行xmlPath.evaluate,类似于xmlPath.evaluate(模式、节点__、XPathConstants.NODE)
发布于 2011-05-31 16:52:03
是的,我认为这是可能的:
URL url = new URL("http://www.w3.org");
// configure JTidy
Tidy tidy = new Tidy();
tidy.setXHTML(true);
tidy.setQuiet(true);
tidy.setXmlOut(true);
tidy.setShowWarnings(false);
Document doc = tidy.parseDOM(url.openConnection().getInputStream(), null);
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr =
xpath.compile("//form[@action = 'http://www.w3.org/Help/search']");
Node form = (Node) expr.evaluate(doc, XPathConstants.NODE);
// create relative XPath
expr = xpath.compile("ul/li[@class = 'last-item']/a");
Node lastItem = (Node) expr.evaluate(form, XPathConstants.NODE);
System.out.println(lastItem.getFirstChild().getNodeValue());返回:
About W3Chttps://stackoverflow.com/questions/6190343
复制相似问题