从 HtmlCleaner 获取文本中经过清理的 HTML

发布于 2024-12-01 05:26:57 字数 104 浏览 1 评论 0原文

我想查看从 HTMLCleaner 获得的清理后的 HTML。 我看到TagNode上有一个名为serialize的方法,但不知道如何使用它。 有人有它的示例代码吗?

谢谢 纳恩

I want to see the cleaned HTML that we get from HTMLCleaner.
I see there is a method called serialize on TagNode, however don't know how to use it.
Does anybody have any sample code for it?

Thanks
Nayn

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

放肆 2024-12-08 05:26:57

这是示例代码:

HtmlCleaner htmlCleaner = new HtmlCleaner();

TagNode root = htmlCleaner.clean(url);

HtmlCleaner.getInnerHtml(root);

String html = "<" + root.getName() + ">" + htmlCleaner.getInnerHtml(root) + "</" + root.getName() + ">";

Here's the sample code:

HtmlCleaner htmlCleaner = new HtmlCleaner();

TagNode root = htmlCleaner.clean(url);

HtmlCleaner.getInnerHtml(root);

String html = "<" + root.getName() + ">" + htmlCleaner.getInnerHtml(root) + "</" + root.getName() + ">";
能怎样 2024-12-08 05:26:57

使用 org.htmlcleaner.XmlSerializer 的子类,例如:

// get the element you want to serialize
HtmlCleaner cleaner     = new HtmlCleaner();
TagNode     rootTagNode = cleaner.clean(url);

// set up properties for the serializer (optional, see online docs)
CleanerProperties cleanerProperties = cleaner.getProperties();
cleanerProperties.setOmitXmlDeclaration(true);

// use the getAsString method on an XmlSerializer class
XmlSerializer xmlSerializer = new PrettyXmlSerializer(cleanerProperties);
String        html          = xmlSerializer.getAsString(rootTagNode);

Use a subclass of org.htmlcleaner.XmlSerializer, for example:

// get the element you want to serialize
HtmlCleaner cleaner     = new HtmlCleaner();
TagNode     rootTagNode = cleaner.clean(url);

// set up properties for the serializer (optional, see online docs)
CleanerProperties cleanerProperties = cleaner.getProperties();
cleanerProperties.setOmitXmlDeclaration(true);

// use the getAsString method on an XmlSerializer class
XmlSerializer xmlSerializer = new PrettyXmlSerializer(cleanerProperties);
String        html          = xmlSerializer.getAsString(rootTagNode);
水溶 2024-12-08 05:26:57
XmlSerializer xmlSerializer = new PrettyXmlSerializer(cleanerProperties);

String html = xmlSerializer.getAsString(rootTagNode);

上面的方法有一个问题,它会修剪html标签中的内容,例如,

这是第 1 段。

 will become 

这是第 1 段。

它是 getSingleLineOfChildren 函数执行修剪操作。因此,如果我们从网站获取数据并希望保持像 tuckunder 这样的格式。

PS:如果html标签有子标签,则父标签内容不会被修剪,

例如

;这是第1段。 www.xxxxx.com

将在“this is paragraph1”之前保留空格

XmlSerializer xmlSerializer = new PrettyXmlSerializer(cleanerProperties);

String html = xmlSerializer.getAsString(rootTagNode);

the method above has a problem,it will trim content in html label, for example,

this is paragraph1.

 will become 

this is paragraph1.

and it is getSingleLineOfChildren function does the trim operation. So if we fetch data from website and want to keep the format like tuckunder.

PS:if a html label has children label,the parent label contetn will not be trimed,

for example <p> this is paragraph1. <a>www.xxxxx.com</a> </p> will keep whitespace before "this is paragraph1"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文