简体   繁体   English

如何删除 java 中 Jtidy 中的警告

[英]how to remove the warnings in Jtidy in java

I am using Jtidy parser in java.我在 java 中使用 Jtidy 解析器。

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);"当我运行它时,“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.请帮助我,如何做到这一点,如何从控制台中删除这些警告。

Looking at the Documentation I found a few methods which may do what you want.查看文档,我发现了一些可以满足您需求的方法。

There is setShowErrors , setQuiet and setErrout .setShowErrorssetQuietsetErrout 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.还有setForceOutput来获取 output,即使产生了错误。

If you want to redirect the JTidy warnings to (say) a log4j logger, read this blog entry .如果您想将 JTidy 警告重定向到(例如)log4j 记录器,请阅读此博客条目

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.如果您只是想让它们远离 go(连同其他控制台输出),则使用System.setOut()和/或System.setErr()将 output 发送到文件......或黑洞。

For JTidy release 8 (or later), the Tidy.setMessageListener(TidyMessageListener) method deals with the messages more gracefully.对于 JTidy 版本 8(或更高版本), Tidy.setMessageListener(TidyMessageListener)方法更优雅地处理消息。


Alternatively, you could send a bug report to webmaster@yahoo.com .或者,您可以向webmaster@yahoo.com发送错误报告。 :-) :-)

Writer out = new NullWriter();
PrintWriter dummyOut = new PrintWriter(out);
tidy.setErrout(dummyOut);

Looking at the documentation I found another method that seems a bit nicer to me in this particular case: setShowWarnings(boolean) .查看文档,我发现在这种特殊情况下对我来说似乎更好的另一种方法: 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 )有关更多信息,请查看此处: http://www.docjar.com/docs/api/org/w3c/tidy/Tidy.html#setShowWarnings(boolean )

I think this is the nicest solution, based on the answer of Joost:根据 Joost 的回答,我认为这是最好的解决方案:

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

All three are necessary.这三个都是必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM