我正在使用 JTidy v. r938。我正在使用这段代码来尝试清理页面……
final Tidy tidy = new Tidy();
tidy.setQuiet(false);
tidy.setShowWarnings(true);
tidy.setShowErrors(0);
tidy.setMakeClean(true);
Document document = tidy.parseDOM(conn.getInputStream(), null);
但是当我解析这个 URL 时 -- http://www.chicagoreader.com/chicago/EventSearch?narrowByDate=This+Week&eventCategory=93922&keywords=&page=1 ,事情没有得到清理。例如,页面上的 META 标记,如
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
保持原样
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
而不是“”标签或显示为“"。我通过将生成的 JTidy org.w3c.dom.Document 作为字符串输出来确认这一点。
我该怎么做才能使 JTidy 真正清理页面——即使其格式正确?我知道那里还有其他工具,但这个问题具体与使用 JTIdy 有关。
最佳答案
如果你想要 XML 格式,你需要为 Tidy 指定几个标志
private String cleanData(String data) throws UnsupportedEncodingException {
Tidy tidy = new Tidy();
tidy.setInputEncoding("UTF-8");
tidy.setOutputEncoding("UTF-8");
tidy.setWraplen(Integer.MAX_VALUE);
tidy.setPrintBodyOnly(true);
tidy.setXmlOut(true);
tidy.setSmartIndent(true);
ByteArrayInputStream inputStream = new ByteArrayInputStream(data.getBytes("UTF-8"));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
tidy.parseDOM(inputStream, outputStream);
return outputStream.toString("UTF-8");
}
或者简单地如果想要 XHTML 形式
Tidy tidy = new Tidy();
tidy.setXHTML(true);
关于java - 我如何使 JTIdy 使 HTML 文档格式正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10390922/