Если вы хотите обернуть все тело в div, попробуйте это:
Element body = doc.select("body").first();
Element div = new Element("div");
div.html(body.html());
body.html(div.outerHtml());
Результат:
<body>
<div>
I am a text that needs to be wrapped in a div!
<div class="...">
...
</div> ... I am more text that needs to be wrapped in a div! ...
</div>
</body>
Если вы хотите обернуть каждый текст в отдельный div, попробуйте это:
Element body = doc.select("body").first();
Element newBody = new Element("body");
for (Node n : body.childNodes()) {
if (n instanceof Element && "div".equals(((Element) n).tagName())) {
newBody.append(n.outerHtml());
} else {
Element div = new Element("div");
div.html(n.outerHtml());
newBody.append(div.outerHtml());
}
}
body.replaceWith(newBody);
<body>
<div>
I am a text that needs to be wrapped in a div!
</div>
<div class="...">
...
</div>
<div>
... I am more text that needs to be wrapped in a div! ...
</div>
</body>