简体   繁体   English

如何使用 Jsoup 添加新的 html 标签?

[英]How to add a new html tag with Jsoup?

I'm having some trouble adding a new tag to my Document.我在向我的文档添加新标签时遇到了一些问题。 For example I have:例如我有:

Document doc = Jsoup.parse(htmlString);
Element table = doc.select("table").first();  

Now If I want to add a <LINK> tag with attributes (href,type,rel) to my table element, and then return the total as a string, How would I do this?现在,如果我想将带有属性(href、type、rel)的<LINK>标记添加到我的表元素,然后将总数作为字符串返回,我该怎么做?

Use something like this:使用这样的东西:

Jsoup.parse(new URL(""), 0).getElementById("test").appendElement("h1").attr("id", "header").text("Welcome");

And all ".append*" methods.以及所有“.append*”方法。

It's been quite some time but I also searched through for it a while so I will share my answer.已经有一段时间了,但我也搜索了一段时间,所以我会分享我的答案。 This is for Jsoup 1.13.1这是 Jsoup 1.13.1


Document doc = Jsoup.parse(htmlString);
Element table = doc.select("table").first();

table.appendElement("link").attr("href","example.com/file.css").attr("rel","stylesheet").attr("type","text/css");


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM