Is there a validating HTML parser implemented in Java?

后端 未结 4 1360
名媛妹妹
名媛妹妹 2021-01-06 08:50

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, the

相关标签:
4条回答
  • 2021-01-06 09:05

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

    0 讨论(0)
  • 2021-01-06 09: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!

    0 讨论(0)
  • 2021-01-06 09:30

    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.

    0 讨论(0)
  • 2021-01-06 09:31

    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

    0 讨论(0)
提交回复
热议问题