Take the 2-minute tour ×

I'm always looking for a modern Java library that makes creating valid (X)HTML snippets easy.

Yes you could use a templating language but there are times when you do not want to do this because Java has some advantages over insert your favorite templating language.

I have seen lots of in-house HTML builders in many projects but there is no Commons-HTML Builder that I can find.

Does anyone know of one?

It would be ideal if it took advantage of the Java 5/6/7 type system (generics) and support Fluent Style. Or something like fluent style ie JQuery style chaining, or a state machine used in mocking libraries like JMock (pedantically speaking a Monad).

A rough builder example might be:

new Html().title("stuff").body().in().div().in().h1("Hello World").hr();

Another example: http://codemonkeyism.com/the-best-markup-builder-i-could-build-in-java/

I ended up writing my own: Java Anti-template Language (JATL)

share|improve this question
Another option is to use JAXB's Fluent Plugin –  Adam Gent Aug 28 '10 at 14:01

3 Answers 3

up vote 30 down vote accepted

I ended up writing my own library called Java Anti-template Language (JATL)

share|improve this answer
Looks good, are you still updating it? –  C. Ross Mar 29 '11 at 13:10
Yep. I need to add HTML 5 soon. Probably add it in a couple of weeks. Its also now in the public maven repo. –  Adam Gent Mar 31 '11 at 15:38
Great job! It would be much better if the inner elements could go inside the constructors like: body(h1(), div(table(...))) –  Xiè Jìléi Sep 26 '11 at 9:27
@XieJilei Yeah I was thinking of adding that in the major release for people that like lisp style S-expressions. There are some evaluation challenges in going that direction (ie you want the body tag to be written before the h1 tag... otherwise you will have to buffer the whole document). –  Adam Gent Oct 18 '11 at 17:09

Have you tried the Jakarta Element Construction Set (ECS) project?

It is not really a fluent API - reminds me more of StringBuilder than Mockito... But functionally I think it is what you're after.

share|improve this answer
This is definitely a step in the right direction (+1) –  Sean Patrick Floyd Aug 27 '10 at 12:06
I think we have a winner. What a horrible non-friendly SEO project name :) Your right its probably not really fluent as it would involve a state machine. If anything its actually more like a Monad. Think JQuery. –  Adam Gent Aug 27 '10 at 12:11
It still seems clunky to me and doesn't support XHTML. –  Adam Gent Aug 27 '10 at 12:18
And it's now no longer being developed. –  Alexey Romanov Oct 13 '11 at 8:08

Your best bet is probably to use an XML library and render the output as HTML.

I.E. Dom4J defines a HtmlWriter class for HTML-specific XML output.

But you'd still have to create your own api on top of it to actually create the document.

share|improve this answer
The annoying probablem with Dom4J its lack of support of generics. ie node.elements() returns a List<?> not List<Element> But is my favorite XML library for Java –  Adam Gent Aug 27 '10 at 12:08
I would prefer to print HTML directly and escape all variables by hand, instead of any XML library. To do with HTML, XML library can make code ugly and extreme long. –  Xiè Jìléi Sep 26 '11 at 9:31

Your Answer


