如何替换HTML元素-不添加根标签?

时间:2020-09-17 13:12:14

标签: java jsoup

当我尝试替换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>

0 个答案:

没有答案