3
我正在嘗試使用TagSoup和XPath(JAXP)。我知道如何從TagSoup(或XMLReader)獲取SAX解析器。但我沒有找到如何創建將使用該SAX解析器的DocumentBuilder。我怎麼做?TagSoup和XPath
謝謝。
編輯:對不起,如此一般,但Java XML API是如此痛苦。
EDIT2:
問題解決了:
public static void main(String[] args) throws XPathExpressionException, IOException,
SAXNotRecognizedException, SAXNotSupportedException,
TransformerFactoryConfigurationError, TransformerException {
XPathFactory xpathFac = XPathFactory.newInstance();
XPath xpath = xpathFac.newXPath();
InputStream input = new FileInputStream("/tmp/g.html");
XMLReader reader = new Parser();
reader.setFeature(Parser.namespacesFeature, false);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMResult result = new DOMResult();
transformer.transform(new SAXSource(reader, new InputSource(input)), result);
Node htmlNode = result.getNode();
NodeList nodes = (NodeList) xpath.evaluate("//span", htmlNode, XPathConstants.NODESET);
System.out.println(nodes.getLength());
}
EDIT3:
鏈接,幫助我:http://www.jezuk.co.uk/cgi-bin/view/jez?id=2643