将结束标记添加到 HtmlElement (HtmlUnit)

发布于 2024-12-23 06:03:24 字数 222 浏览 3 评论 0原文

我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与之呼应 2024-12-30 06:03:24

您可以使用 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.

青衫儰鉨ミ守葔 2024-12-30 06:03:24

实际上这些标签不应该有结束标签。看看这个:

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

回忆躺在深渊里 2024-12-30 06:03:24

谢谢各位的解答,我错了。问题不在于这些 html 标签。因为我要转换的html页面包含非法的xml字符,因此我无法转换它。

我通过使用 Xerces 库的 XMLChar.isValid 方法找到了解决方案。
此外,我必须使用以下代码删除 htmlpage 的所有 scriptstyle 标签:

final DomNodeList<HtmlElement> scriptElements = html.getElementsByTagName("script");
if (!scriptElements.isEmpty()){
    for (HtmlElement scriptElement : scriptElements){   
        scriptElement.removeAllChildren();
    }
}

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 :

final DomNodeList<HtmlElement> scriptElements = html.getElementsByTagName("script");
if (!scriptElements.isEmpty()){
    for (HtmlElement scriptElement : scriptElements){   
        scriptElement.removeAllChildren();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文