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?
Jsoup is an open source Java library used mainly for extracting data from HTML. It also allows you to manipulate and output HTML. It has a steady development line, great documentation, and a fluent and flexible API. Jsoup can also be used to parse and build XML.
What It Is. jsoup can parse HTML files, input streams, URLs, or even strings. It eases data extraction from HTML by offering Document Object Model (DOM) traversal methods and CSS and jQuery-like selectors. jsoup can manipulate the content: the HTML element itself, its attributes, or its text.
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(); }
You should tell that syntax you want to leave the string in HTML or XML.
public String parserXHtml(String html) { org.jsoup.nodes.Document document = Jsoup.parseBodyFragment(html); document.outputSettings().syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml); //This will ensure the validity document.outputSettings().charset("UTF-8"); return document.toString(); }
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