A good HTML object model in Java?

Guillaume picture Guillaume · Jan 22, 2010 · Viewed 8.4k times · Source

I'm looking for an HTML object model in Java, capable of parsing HTML (not required) and containing all HTML elements (and CSS as well) in an elegant object model.

I'm looking for a pure java version of the Groovy's HTML builder. (I have no luck on google with this request.)

I want to be able to perform stuff like:

HTML html = new HTML();
Body body = html.body();

Table table body.addTable(myCssStyle);
Row row = table.addRow("a", "b", "c").withCss(cssRowStyle);

and so on...

Answer

ollo picture ollo · Jan 23, 2013

Check out Jsoup:

Example: (Building some html)

Document doc = Document.createShell("");

Element headline = doc.body().appendElement("h1").text("thats a headline");
Element pTag = doc.body().appendElement("p").text("some text ...");
Element span = pTag.prependElement("span").text("That's");

System.out.println(doc);

Output:

<html>
 <head></head>
 <body>
  <h1>thats a headline</h1>
  <p><span>That's</span>some text ...</p>
 </body>
</html>

Documentation: