问题
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