【问题标题】:How to add a new html tag with Jsoup?如何使用 Jsoup 添加新的 html 标签?
【发布时间】:2011-11-27 21:30:26
【问题描述】:

我在向我的文档添加新标签时遇到了一些问题。例如我有:

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

现在如果我想在我的表格元素中添加一个带有属性 (href,type,rel) 的<LINK>tag,然后将总数作为字符串返回,我该怎么做?

【问题讨论】:

  • ^^ 我在将一个添加到另一个(也)时遇到问题。
  • 现在您必须分享您遇到的问题是什么?因为在我分享的链接中给出了向元素添加标签和属性的过程。
  • @djaqeel 该链接没有说明实际添加标签的任何内容,仅涉及更改其属性。 . .
  • table.append("example.com/your.css\" />");没用吗?

标签: java android html dom jsoup


【解决方案1】:

使用这样的东西:

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

以及所有“.append*”方法。

【讨论】:

  • 嗨,伙计,这不起作用。我得到了以下异常:java.net.MalformedURLException: no protocol:.
【解决方案2】:

已经有一段时间了,但我也搜索了一段时间,所以我会分享我的答案。这是针对 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");


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多