我在用JTidy诉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有关。
发布于 2012-05-01 06:04:42
如果您想要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);发布于 2013-02-13 00:22:35
使用tidy.setXmlTags(true);解析XML而不是HTML
发布于 2013-08-06 05:27:31
即使发现错误,也要使用Tidy.setForceOutput(true) (风险自负)生成输出。
https://stackoverflow.com/questions/10390922
复制相似问题