So, I whip up a quick test case in Java 7 to grab a couple of elements from random URIs, and see if the built-in parsing stuff will do what I need.

Here's the basic setup (with exception handling etc omitted):

DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder dbuild = dbfac.newDocumentBuilder();
Document doc = dbuild.parse("uri-goes-here");

With no error handler installed, the parse method throws exceptions on fatal parse errors.

When getting the standard Apache 2.2 directory index page from a local server: a SAXParseException with the message White spaces are required between publicId and systemId. The doctype looks ok to me, whitespace and all.

When getting a page off a Drupal 7 generated site, it never finishes. The parse method seems to hang. No exceptions thrown, never returns.

When getting http://www.oracle.com, a SAXParseException with the message The element type "meta" must be terminated by the matching end-tag "</meta>".

So it would appear that the default setup I've used here doesn't handle HTML, only strictly written XML.

My question is: can JAXP be used out-of-the-box from openJDK 7 to parse HTML from the wild (without insane gesticulations), or am I better off looking for an HTML 5 parser?

PS this is for something I may not open-source, so licensing is also an issue :(

1 Answer 1


This Stack Overflow answer has a couple of solutions. JAXP apparently only works on well formed XML. You'd need to convert the HTML to XHTML before subjecting it to the JAXP's standard parser.

  • Thanks, that pretty much sums up what I'd surmised, that you need well formed XML for JAXP. I'll look through and find something quick-n-dirty, either a converter or html parser. Or even (shudder) cruft a partial parser :)
    – ikmac
    Commented Oct 3, 2012 at 19:31
  • As long as you don't use Regular Expressions, you should be fine.
    – user28988
    Commented Oct 3, 2012 at 19:38
  • Yes, I only want one problem at a time :D
    – ikmac
    Commented Oct 5, 2012 at 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.