【问题标题】:How can I get the error/warning messages out of the parsed HTML using JTidy?如何使用 JTidy 从解析的 HTML 中获取错误/警告消息?
【发布时间】:2011-01-28 04:45:06
【问题描述】:

我能够解析 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());

【问题讨论】:

    标签: html validation xhtml tidy


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      这里是doc

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-15
        • 1970-01-01
        • 2021-08-30
        • 2015-10-25
        • 1970-01-01
        • 2016-09-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多