Search code examples
javahtmlxhtmljsoup

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


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?


Solution

  • 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();
    }