I'm trying to update an html document so that all the contents of the body are wrapped into a table cell. Let's say the original html looks similar to
String test= "<html><body><p class=default><span>my content</span></p><p>my other content</p></body></html>";
I want to convert that into,
"<html><body><table><tr><td><p class=default><span>my content</span></p><p>my other content</p></td></tr></table></body></html>";
I'm trying to implement it this way but it is not working as expected.
Document ht = Jsoup.parse(test);
String updatedHtml = "<table><tr><td></td>" + ht.body().html() + "</td></tr></table>";
ht.body().html(updatedHtml);
System.out.println(ht.body().html());
What I get looks similar to the content pasted here. i.e. the body content is showing up outside of the table cell. How do I fix this issue?
<p class="default"><span>my content</span></p>
<p>my other content</p>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
Your html in the
updatedHtmlstring is not valid - you've got two closingtdtags, and that is the cause of the problem. Remove the first closing tag:String updatedHtml = "<table><tr><td>" + ht.body().html() + "</td></tr></table>";And the output now is: