jTidy - 漂亮的无头打印,标题标签
我正在尝试使用 jTidy 来漂亮地打印我拥有的 HTML 片段。到目前为止我已经做了以下事情。
protected String prettyPrintHTML(String rawHTML) {
Tidy tidy = new Tidy();
tidy.setXHTML(true);
tidy.setIndentContent(true);
tidy.setPrintBodyOnly(true);
tidy.setTidyMark(false);
// Convert HTML to DOM
Document htmlDOM = tidy.parseDOM(new ByteArrayInputStream(rawHTML.getBytes()), null);
// Pretty Print
OutputStream out = new ByteArrayOutputStream();
tidy.pprint(htmlDOM, out);
return out.toString();
}
这是可行的,但输出包括附加内容,例如 、、
例如,对于输入
- sub1
- sub2
- sub21
line 1 column 59 - Warning: inserting missing 'title' element
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
</head>
<body>
<ul>
<li>
sub1
</li>
<li>
sub2
<ul>
<li>
sub21
</li>
</ul>
</li>
</ul>
</body>
</html>
浏览 API 后,我找到了 setPrintBodyOnly 方法(我在上面使用过),但仍然没有运气。
我在这里缺少什么?我尝试过谷歌搜索,但到目前为止所有链接都通向死胡同。
I am trying to use jTidy to pretty print on a HTML snippet that I have. So far I have done the following.
protected String prettyPrintHTML(String rawHTML) {
Tidy tidy = new Tidy();
tidy.setXHTML(true);
tidy.setIndentContent(true);
tidy.setPrintBodyOnly(true);
tidy.setTidyMark(false);
// Convert HTML to DOM
Document htmlDOM = tidy.parseDOM(new ByteArrayInputStream(rawHTML.getBytes()), null);
// Pretty Print
OutputStream out = new ByteArrayOutputStream();
tidy.pprint(htmlDOM, out);
return out.toString();
}
This works, but the output includes additional content such as <html>, <head>, <title> and <body> tags.
For example, for input <ul><li>sub1</li><li>sub2<ul><li>sub21</li></ul></li></ul> this gives,
line 1 column 59 - Warning: inserting missing 'title' element
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
</head>
<body>
<ul>
<li>
sub1
</li>
<li>
sub2
<ul>
<li>
sub21
</li>
</ul>
</li>
</ul>
</body>
</html>
After skimming through the API, I found the setPrintBodyOnly
method (which I have used above), but still no luck.
What am I missing here ? I tried Googling but so far all links lead to dead ends.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下链接中发布了类似的问题。它的答案显示了一些实现相同目标的替代方法。
JTidy Node.findBody() — 如何使用?
There is a similar question posted in the following link. It has answers which show some alternate ways to achieve the same.
JTidy Node.findBody() — How to use?