我在使用jTidy (在安卓系统上)时遇到了一个非常恼人的问题。我发现jTidy在我测试过的每个超文本标记语言文档上都能工作,除了以下几个文档:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<!-- Always force latest IE rendering engine & Chrome Frame
Remove this if you use the .htaccess -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>templates</title>
<meta name="description" content="" />
<meta name="author" content="" />
<meta name="viewport" content="width=device-width; initial-scale=1.0" />
<!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
</head>
<body>
<div>
<header>
<h1>Page Heading</h1>
</header>
<nav>
<p><a href="/">Home</a></p>
<p><a href="/contact">Contact</a></p>
</nav>
<div>
</div>
<footer>
<p>© Copyright</p>
</footer>
</div>
</body>
</html>但在对其进行整理后,jTidy不会返回任何内容(例如,如果包含整理后的HTML的字符串称为result,则result.equals("") == true)
我注意到一件非常有趣的事情:如果我去掉了HTML中的所有内容,jTidy就能完美地工作。jTidy中有什么不喜欢的地方吗?
下面是我使用的Java代码:
public String tidy(String sourceHTML) {
StringReader reader = new StringReader(sourceHTML);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Tidy tidy = new Tidy();
tidy.setMakeClean(true);
tidy.setQuiet(false);
tidy.setIndentContent(true);
tidy.setSmartIndent(true);
tidy.parse(reader, baos);
try {
return baos.toString(mEncoding);
} catch (UnsupportedEncodingException e) {
return null;
}
}我的Java有什么问题吗?这是jTidy的错误吗?有没有办法让jTidy不这么做?(我不能更改HTML)。如果这个问题绝对不能解决,有没有其他好的HTML Tidiers?非常感谢!
发布于 2013-06-18 00:56:18
试试这个:
tidy.setForceOutput(true);可能存在解析错误。
发布于 2013-02-11 03:07:25
查看Jsoup,这是我对任何类型的Java Html处理的推荐(我曾经使用过HtmlCleaner,但后来切换到了jsoup)。
使用Jsoup:清理
final String yourHtml = ...
String output = Jsoup.clean(yourHtml, Whitelist.relaxed());这就是全部!
或(如果您想要更改/删除/解析/ ...)一些东西:
Document doc = Jsoup.parse(<file/string/website>, null);
String output = doc.toString();https://stackoverflow.com/questions/8885586
复制相似问题