如果要将整个身体包裹在一个分区中,请尝试以下操作:
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>
如果要将每个文本单独包装,请尝试以下操作:
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>