Good day,
How to use jsoup to replace html element code?
For example div on <!--div-->
Input code <body><div>Текст <div> Текст 2</div></div></body>
On <body><div>Текст <!--div--></div></body>

    1 answer 1

    Something like this:

     String html = "<body><div>Текст <div> Текст 2</div></div><div>Текст33 <div> Текст 44</div></div></body>"; 

    take the elements

     Document document = Jsoup.parse(html); Elements els = document.select("div > div:contains(Текст 2)"); for (Element el : els) { el.replaceWith(new TextNode("<!--div-->", "")); } System.out.println(document); 

    or

     Document document = Jsoup.parse(html); Elements els = document.select("div > div"); for (Element el : els) { if (el.text().contains("Текст 2")) el.replaceWith(new TextNode("<!--div-->", "")); } System.out.println(document);