tags:

views:

134

answers:

3

Is there any recommended Java library for generating HTML code programatically?

For example, I'd like to do something like this:

// Creates table with 1 row and 2 columns
HTMLTable table = new HTMLBuilder().getHTMLTable(1, 2); 

// Sets the table contents
table.setCell(0,0, "abc");
table.setCell(0,1, "def");

String html = table.getHTML();

HTMLTable and HTMLBuilder are classes that the library would provide.

+4  A: 

There's the Apache Jakarta ECS library. It claims to be HTML 4.0 compatible. Table Javadoc here.

Another solution may be to use a templating library. Freemarker is particularly good (and powerful). Velocity is less powerful but perhaps simpler.

Brian Agnew
+1  A: 

I'm not sure if you're looking solely for tables or if that was just a convenient example, but have you checked out javax.swing.text.html.HTMLDocument and classes related to it? They're in the API here.

Lord Torgamus
+1  A: 

Here's one.

Jonathan Feinberg