当我尝试替换HTML元素时,将其包装在<root>
标记中。如何避免呢?
String htmlPage = "<div class=\"xxxx\" id=\"dummyId\">dummy html content</div>";
Document document = Jsoup.parse(htmlPage, "", Parser.htmlParser());
Element elementById = document.getElementById("dummyId");
Element elementToReplace = Jsoup.parse("<div>html-code-to-replace</div>", "", Parser.htmlParser());
elementById.replaceWith(elementToReplace);
结果
<html>
<head></head>
<body>
<#root>
<html>
<head></head>
<body>
<div>
html-code-to-replace
</div>
</body>
</html>
</#root>
</body>
</html>