Is it possible to convert HTML into XHTML with Jsoup 1.8.1?

Henry picture Henry · Mar 16, 2015 · Viewed 10.1k times · Source
String body = "<br>";
Document document = Jsoup.parseBodyFragment(body);
document.outputSettings().escapeMode(EscapeMode.xhtml);
String str = document.body().html();
System.out.println(str);

expect: <br />

result: <br>

Can Jsoup convert value HTML into XHTML?

Answer

Henry picture Henry · Mar 16, 2015

See Document.OutputSettings.Syntax.xml:

private String toXHTML( String html ) {
    final Document document = Jsoup.parse(html);
    document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);    
    return document.html();
}