I converted an html string to xhtml in java using jsoup as shown here: Is it possible to convert HTML into XHTML with Jsoup 1.8.1?
However, I couldn't find a way to do the opposite, I mean, convert xhtml to html; is there a way to do this in java?
Using Document.OutputSettings.Syntax.html
instead of Document.OutputSettings.Syntax.xml
you can use the example code from that stackoverflow question to convert xhtml back to html.
import org.jsoup.parser.Parser;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Entities.EscapeMode;
import org.jsoup.Jsoup;
public class MyClass {
public static void main(String args[]) {
String body = "<br>";
Document document = Jsoup.parseBodyFragment(body);
document.outputSettings().escapeMode(EscapeMode.xhtml);
String str = document.body().html();
System.out.println("XHTML:\n"+toXHTML(str));
System.out.println("\nHTML:\n"+toHTML(str));
}
static String toHTML( String html ) {
final Document document = Jsoup.parse(html);
document.outputSettings().syntax(Document.OutputSettings.Syntax.html);
return document.html();
}
static String toXHTML( String html ) {
final Document document = Jsoup.parse(html);
document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
return document.html();
}
}
XHTML:
<html>
<head></head>
<body>
<br />
</body>
</html>
HTML:
<html>
<head></head>
<body>
<br>
</body>
</html>
References:
Is it possible to convert HTML into XHTML with Jsoup 1.8.1? - Henry
Jsoup document settings
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.