Is there a validating HTML parser implemented in J

2019-02-14 19:38发布

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?

标签: java html xhtml
4条回答
SAY GOODBYE
2楼-- · 2019-02-14 20:04

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

查看更多
虎瘦雄心在
3楼-- · 2019-02-14 20:09

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!

查看更多
来,给爷笑一个
4楼-- · 2019-02-14 20:09

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.

查看更多
唯我独甜
5楼-- · 2019-02-14 20:10

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...

查看更多
登录 后发表回答