4

I am using Jtidy parser in java.

URL url = new URL("www.yahoo.com"); 
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream in = conn.getInputStream(); 
doc = new Tidy().parseDOM(in, null);

when I run this, "doc = new Tidy().parseDOM(in, null);" I am getting some warnings as follows:

Tidy (vers 4th August 2000) Parsing "InputStream"
line 140 column 5 - Warning: <table> lacks "summary" attribute

InputStream: Doctype given is "-//W3C//DTD XHTML 1.0 Strict//EN"
InputStream: Document content looks like HTML 4.01 Transitional

1 warnings/errors were found!

These warnings are getting displayed automatically on console. But I don't want these warnings to be displayed on my console after running

doc = new Tidy().parseDOM(in, null);

Please help me,how to do this,how to remove these warnings from console.

Mat
  • 202,337
  • 40
  • 393
  • 406
DJ31
  • 1,219
  • 3
  • 14
  • 19

5 Answers5

11

Looking at the Documentation I found a few methods which may do what you want.

There is setShowErrors, setQuiet and setErrout. You may want to try the following:

Tidy tidy = new Tidy();
tidy.setShowErrors(0);
tidy.setQuiet(true);
tidy.setErrout(null);
doc = tidy.parseDOM(in, null);

One of them may be enough already, these were all the options I found. Note that this will simply hide the messages, not do anything about them. There is also setForceOutput to get the output, even if errors were generated.

Joost
  • 10,333
  • 4
  • 55
  • 61
5

If you want to redirect the JTidy warnings to (say) a log4j logger, read this blog entry.

If you simply want them to go away (along with other console output), then use System.setOut() and/or System.setErr() to send the output to a file ... or a black hole.

For JTidy release 8 (or later), the Tidy.setMessageListener(TidyMessageListener) method deals with the messages more gracefully.


Alternatively, you could send a bug report to webmaster@yahoo.com. :-)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
3
Writer out = new NullWriter();
PrintWriter dummyOut = new PrintWriter(out);
tidy.setErrout(dummyOut);
Brian
  • 31
  • 1
2

Looking at the documentation I found another method that seems a bit nicer to me in this particular case: setShowWarnings(boolean). This method will hide the warnings, but errors will still be thrown.

For more info look here: http://www.docjar.com/docs/api/org/w3c/tidy/Tidy.html#setShowWarnings(boolean)

Aloys
  • 682
  • 4
  • 11
0

I think this is the nicest solution, based on the answer of Joost:

Tidy tidy = new Tidy();
tidy.setShowErrors(0);
tidy.setShowWarnings(false);
tidy.setQuiet(true);

All three are necessary.

user42723
  • 467
  • 3
  • 8