Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 656
Henry Avatar asked Mar 16 '15 21:03

Henry


People also ask

Where is jsoup used?

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 does jsoup parse do?

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.


2 Answers

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(); } 
like image 196
Henry Avatar answered Sep 29 '22 16:09

Henry


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();     } 
like image 42
cdcaiza Avatar answered Sep 29 '22 16:09

cdcaiza