4

I need to parse HTML 4 in Java. Ideally I'd like an implementation that is SAX compatible.

I'm aware that there are numerous HTML parsers in for Java, however, they all seem to perform 'tidying'. In other words, they will correct badly formed HTML. I don't want this.

My requirements are:

  1. No tidying.
  2. If the input document is invalid HTML parsing should fail.
  3. The document should be validatable against the HTML DTDs.
  4. The parser can produce SAX2 events.

Is there a library that meets these requirements?

3
  • If the parser doesn't tidy, it can't create a DOM tree; a valid HTML document may not be valid XML document (e.g., think of all those <p> tags that have no corresponding closing tags). Commented May 24, 2009 at 20:33
  • It could fire SAX events as if it were a <p/> xml element - right? Commented May 25, 2009 at 8:16
  • How would it know that the close tag was missing? Commented May 27, 2009 at 3:27

4 Answers 4

2

You can find a collection of HTML parsers here HTML Parsers. I don't remeber exactly but I think TagSoup parses the file without applying corrections...

Sign up to request clarification or add additional context in comments.

4 Comments

"TagSoup, a SAX-compliant parser written in Java that, instead of parsing well-formed or valid XML, parses HTML as it is found in the wild..." Unfortunately not.
"It does guarantee well-structured results: tags will wind up properly nested, default attributes will appear appropriately, and so on."
If it is able to populate default attributes this mean it parses the DTD...it's not clear if it fails if the document fails to be validated.
Also have a look at javax.swing.text.html.parser.Parser, it looks like it does DTD validation protected void endTag(boolean omitted) { handleText(stack.tag); if (omitted && !stack.elem.omitEnd()) { error("end.missing", stack.elem.getName()); } else if (!stack.terminate()) { error("end.unexpected", stack.elem.getName()); }
2

I think the Jericho HTML Parser can deliver at least one of your core requirements ('If the input document is invalid HTML parsing should fail.') in that it will at least tell you if there are mismatched tags or other poisonous HTML flaws, and you can choose to fail based on this information.

Try typing invalid html into this Jericho formatting demo, and note the 'Parser Log' at the bottom of the page:

http://jerichohtmlparser.appspot.com/samples/FormatSource.jsp

So yes, this is doing tag tidying, but it is at least telling you about it - you can grab this information by setting a net.htmlparser.jericho.Logger (e.g. a WriterLogger or something more specific of your own creation) on your source, and then proceeding depending on what errors are logged out. This is a small example:

    Source source=new Source("<a>I forgot to close my link!");
    source.setLogger(myListeningLogger);

    source.getSourceFormatter().writeTo(new NullWriter());
    // myListeningLogger has now had all the HTML flaws written to it

In the example above, your logger's info() method is called with the string: 'StartTag at (r1,c1,p0) missing required end tag', which is relatively parseable, and you can always decide to just reject any HTML that logs any message worse than debug - in fact Jericho logs almost all errors as 'info' level, with a couple at 'warn' level (you might be tempted to create a small fork with the severities adjusted to correspond to what you care about).

Jericho is available on Maven Central, which is always a good sign:

http://mvnrepository.com/artifact/net.htmlparser.jericho/jericho-html

Good luck!

Comments

1

You may wish to check http://lobobrowser.org/cobra.jsp. They have a pure Java web browser (Lobo) implemented. They have the parser component (Cobra) pulled out separately for use. I honestly am not sure if it will do what you require with the "no tidying" requirement, but it may be worth a look. I ran across it when exploring the wild for a pure Java web browser.

Comments

0

You can try to subclass javax.swing.text.html.parser.Parser and implement the handleXXX() methods. It seems it doesn't try to fix the XML. See more at the API

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.