2011-05-31 94 views
1

我正在使用JTidy来解析网页数据。 我的问题是以下几点:JTidy节点处理

可以在以前检索的节点上调用XPath.evalate方法吗?

我会解释得更好。 通常使用xmlPath.evaluate(pattern,document,XPathConstants.NODE)方法调用来检索与xpath表达式匹配的节点列表。

一旦谓我已经检索到的节点或节点列表,我该怎么办xmlPath.evaluate从以前开始检索节点,类似于 xmlPath.evaluate(图案的东西,节点,XPathConstants中。 NODE)

回答

2

是的,我认为这是可能的:

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