How can I get the error/warning messages out of the parsed HTML using JTidy?

做~自己de王妃 提交于 2020-01-02 11:06:15

问题


I am able to parse the HTML but I want to extract the warning messages from the parsed HTML and show them to the user.

Here is my code:

Tidy tidy = new Tidy();
StringBuffer StringBuffer1 = new StringBuffer("<b>Hello<u><b>I am tsting another one.....<i>another.....");
InputStream in = new ByteArrayInputStream(StringBuffer1.toString().getBytes("UTF-8"));
Writer stringWriter = new StringWriter();
    tidy.setPrintBodyOnly(true);
    tidy.setQuiet(true);
    tidy.setShowWarnings(true);
    tidy.setTidyMark(false);
    tidy.setXHTML(true);
    tidy.setXmlTags(false);
    Node parsedNode = tidy.parse(in, stringWriter);
    System.out.print(stringWriter.toString());

回答1:


You can setup an error output stream like this:

errorOutputStream = new java.io.ByteArrayOutputStream();
errorPrintWriter = new java.io.PrintWriter(errorOutputStream, true); //second param enables autoflush so you don't have to manually flush the printWriter
tidy.setErrout(errorPrintWriter);

Then when you need to see the errors errorOutputStream.toString();




回答2:


I noticed it in the jTidy documentation that starting from release r8 jTidy privdes TidyMessageListener interface you can implement to be notified for warning and errors in your html code.

Here is the doc



来源:https://stackoverflow.com/questions/2455980/how-can-i-get-the-error-warning-messages-out-of-the-parsed-html-using-jtidy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!