title: TagSoup and XPath
body: <p>I'm trying to use TagSoup with XPath (JAXP). I know how to obtain SAX parser from TagSoup (or XMLReader). But I failed to find how to create DocumentBuilder that will use that SAX parser. How do I do that?</p>
  
  <p>Thank you.</p>
  
  <p>EDIT: Sorry for being so general but Java XML API is such a pain.</p>
  
  <p>EDIT2:</p>
  
  <p>Problem solved:</p>
  
  <pre><code>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());
  }
  </code></pre>
  
  <p>EDIT3:</p>
  
  <p>Link that helped me: <a href="http://www.jezuk.co.uk/cgi-bin/view/jez?id=2643" rel="nofollow">http://www.jezuk.co.uk/cgi-bin/view/jez?id=2643</a></p>