2011-07-21 6 views
3

私はXPath(JAXP)でTagSoupを使用しようとしています。私はTagSoup(またはXMLReader)からSAXパーサを取得する方法を知っています。しかし、私はそのSAXパーサを使用するDocumentBuilderの作成方法を見つけることができませんでした。それ、どうやったら出来るの?TagSoupとXPath

ありがとうございます。

EDIT:とても一般的ですが、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());