Consider this sample code-
<div>
<outer-tag> Some Text <inner-tag> Some more text</inner-tag></outer-tag>
</div>
I want to get the following output -
<div>
<inner-tag> Some more text</inner-tag>
</div>
How do I achieve this? Thanks!
This solution will work for your current example:
String html = "<div>"
+ "<outer-tag> Some Text <inner-tag> Some more text</inner-tag></outer-tag>"
+ "</div>";
Document doc = Jsoup.parseBodyFragment(html);
for (Element _div : doc.select("div")) {
// get the unwanted outer-tag
Element outerTag = _div.select("outer-tag").first();
// delete any TextNodes that are within outer-tag
for (Node child : outerTag.childNodes()) {
if (child instanceof TextNode) child.remove();
}
// unwrap to remove outer-tag and move inner-tag to child of parent div
outerTag.unwrap();
// print the result
System.out.println(_div);
}
Result is:
<div>
<inner-tag>
Some more text
</inner-tag>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With