JTidy节点处理

| 我正在使用JTidy来解析网页数据。 我的问题如下: 是否可以在先前检索到的节点上调用XPath.evalate方法? 我会解释得更好。 通常,您使用xmlPath.evaluate(pattern,document,XPathConstants.NODE)方法调用来检索与xpath表达式匹配的节点列表。 一旦检索到节点或nodeList,如何从上一个检索到的节点开始xmlPath.evaluate,类似于 xmlPath.evaluate(pattern,node,XPathConstants.NODE)     
已邀请:
        是的,我认为有可能:
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 W3C
    

要回复问题请先登录注册