2011-07-21 53 views
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

回答

1

Java的XML API是這樣的痛苦

事實確實如此。考慮轉向XSLT 2.0/XPath 2.0並改用Saxon的s9api接口。它看起來大致如下:

Processor proc = new Processor(); 

InputStream input = new FileInputStream("/tmp/g.html"); 
XMLReader reader = new Parser(); 
reader.setFeature(Parser.namespacesFeature, false); 
Source source = new SAXSource(parser, input); 

DocumentBuilder builder = proc.newDocumentBuilder(); 
XdmNode input = builder.build(source); 

XPathCompiler compiler = proc.newXPathCompiler(); 
XdmValue result = compiler.evaluate("//span", input); 
System.out.println(result.size());