【问题标题】:JSOUP Element.html("<th>test</th>") ignore th tagsJSOUP Element.html("<th>test</th>") 忽略 th 标签
【发布时间】:2016-10-16 11:16:49
【问题描述】:

我在一个基于 jsoup 的 html 模板引擎上工作。 如果元素不在表内,JSOUP 会忽略 th 和 td 标志;

为了解决这个问题,我将解析器更改为:

    final Document docToWrite = Jsoup.parse(docToRead.outerHtml(),"", Parser.xmlParser());

但我没有找到任何解决方案来用包含 td 或 th 的 html 填充元素:

    element.html("<th>test</th>");

仅返回测试,因为 JSOUP 正在通过删除未使用的标签来清理 html

我该如何解决这个问题?

谢谢

【问题讨论】:

  • 你的元素来自哪里?它是什么标签(element.tag())?

标签: java parsing jsoup


【解决方案1】:

如果你的元素是'th',那么调用:

element.html("<th>test</th>") // th.innerHTML = "<th>test</th>"

应该产生脏的html:

<th><th>test</th></th>

由JSoup正确清除到:

<th>test</th> // th.innerHTML == "test"

要使用 innerHTML == "

test" 填充元素,您的元素必须是 标记。
// Given
String s = "<th>test</th>";
assert element.tag() == "tr";

// When
element.html(s);

// Then
assert element.html().equals(s);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    相关资源
    最近更新 更多