Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use jsoup to tidy up the html

Tags:

jsoup

I am using jsoup and it is really nice to tidy up some html, but I have a piece of invalid html as following:

<p>The recurrence, in close succession <ul><li>list item 1</li><li>list item 2</li></ul> second part of thisssss

What I want to get is :

<p>The recurrence, in close succession </p><ul><li>list item 1</li><li>list item 2</li></ul> <p>second part of thisssss</p>

So is the jsoup capable of tidying up the html and return this output ?

thanks

like image 459
user1505929 Avatar asked Jul 06 '12 06:07

user1505929


1 Answers

Yes, try this:

String html = "<p>The recurrence, in close succession <ul><li>list item 1</li><li>list item 2</li></ul> second part of thisssss";
String clean = Jsoup.clean(html, Whitelist.relaxed());

You can use another Whitelist as well.

like image 172
ollo Avatar answered Oct 23 '22 14:10

ollo