2

我能够解析 HTML,但我想从解析的 HTML 中提取警告消息并将它们显示给用户。

这是我的代码:

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());
4

2 回答 2

2

我在 jTidy 文档中注意到,从发布 r8 jTidy privdes TidyMessageListener 接口开始,您可以实现在 html 代码中收到警告和错误通知。

这是文档

于 2010-03-24T14:39:12.113 回答
2

您可以像这样设置错误输出流:

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);

然后当你需要查看错误时errorOutputStream.toString();

于 2013-06-17T15:54:08.257 回答