将结束标记添加到 HtmlElement (HtmlUnit)
我想将 Htmlpage 转换为 pdf。但 HtmlPage 有很多标签尚未关闭:
< hr >
< br >
因此我无法创建 Pdf。如何在 Java 上使用 HtmlUnit 来关闭这些标签。我想要的是:
<hr />
<br />
谢谢
I want to convert a Htmlpage to pdf. But HtmlPage has lots of tags that haven't been closed as:
< hr >
< br >
Therefore I couldn't create Pdf. How can I close these tags by using HtmlUnit on Java. What I want is :
<hr />
<br />
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 HTML Tidy 来更正 HTML。 Java 库是 JTidy。 JTidy 可以配置为生成 XHTML。
You can use HTML Tidy to correct the HTML. The Java library is JTidy. JTidy can be configured to produce XHTML.
实际上这些标签不应该有结束标签。看看这个:
http://www.w3schools.com/html5/tag_br.asp
http://www.w3schools.com/html5/tag_hr.asp
Actually those tags shouldn't have a closing tag. Take a look at this:
http://www.w3schools.com/html5/tag_br.asp
http://www.w3schools.com/html5/tag_hr.asp
谢谢各位的解答,我错了。问题不在于这些 html 标签。因为我要转换的html页面包含非法的xml字符,因此我无法转换它。
我通过使用 Xerces 库的 XMLChar.isValid 方法找到了解决方案。
此外,我必须使用以下代码删除 htmlpage 的所有 script 和 style 标签:
Thanks for answers, I was wrong. The issue was not about these html tags. Because the htmlpage that I wanted to convert contained illegal xml characters, therefore I couldn't convert it.
I found the solution by using XMLChar.isValid method of Xerces library.
In addition, I had to remove all script and style tags of htmlpage with this code :