2010-03-16 86 views
2

我能夠解析HTML,但我想從解析的HTML中提取警告消息並將其顯示給用戶。如何使用JTidy從解析的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()); 

回答

1

我注意到了這一點,從發佈R8 jTidy開始privdes可以實現TidyMessageListener接口,爲您的HTML代碼錯誤和警告通知的jTidy文檔。

這裏是doc

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