Formatting HTML Output

Other topics

Remarks:

Display all elements as block

By default, Jsoup will display only block-level elements with a trailing line break. Inline elements are displayed without a line break.

Given a body fragment, with inline elements:

<select name="menu">
    <option value="foo">foo</option>
    <option value="bar">bar</option>
</select>

Printing with Jsoup:

Document doc = Jsoup.parse(html);

System.out.println(doc.html());

Results in:

<html>
 <head></head>
 <body>
  <select name="menu"> <option value="foo">foo</option> <option value="bar">bar</option> </select> 
 </body>
</html>

To display the output with each element treated as a block element, the outline option has to be enabled on the document's OutputSettings.

Document doc = Jsoup.parse(html);

doc.outputSettings().outline(true);

System.out.println(doc.html());

Output

<html>
 <head></head>
 <body>
  <select name="menu"> 
   <option value="foo">foo</option> 
   <option value="bar">bar</option> 
  </select> 
 </body>
</html>

Source: JSoup - Formatting the <option> elements

Parameters:

ParameterDetail
boolean outline()Get if outline mode is enabled. Default is false. If enabled, the HTML output methods will consider all tags as block.
Document.OutputSettings outline(boolean)Enable or disable HTML outline mode.

Contributors

Topic Id: 5954

Example Ids: 20874

This site is not affiliated with any of the contributors.