Generate HTML elements with Java


I was looking for a ASP.NET equivalent in Java for generating HTML elements.  I found a nice Apache package to do this.  It is called the Element Construction Set.  http://jakarta.apache.org/ecs/index.html

Javadocs for the package can be found here: http://www.docjar.org/docs/api/org/apache/ecs/html/

This will greatly simplify generating HTML, particularly in a struts tag library or anywhere not in a jsp.

Example usage from http://jakarta.apache.org/ecs/index.html:

Document doc = (Document) new Document()
              .appendTitle("Demo")
              .appendBody(new H1("Demo Header"))
              .appendBody(new H3("Sub Header:"))
              .appendBody(new Font().setSize("+1")
                         .setColor(HtmlColor.WHITE)
                         .setFace("Times")
                         .addElement("The big dog & the little cat chased each other."));
out.println(doc.toString());
// or write to the outputstream directly
output(out);

Generates the equivalent to:

out.println("<HTML>");
out.println("<HEAD><TITLE>Demo<TITLE><HEAD>");
out.println("<BODY>");
out.println("<H1>Demo Header<H1>");
out.println("<H3>Sub Header:<H3>");
out.println("<FONT SIZE=\"+1\" FACE=\"Times\" COLOR=\"#FFFFFF">);
out.println("The big dog &amp; the little cat chased each other.");
out.println("<FONT>");
out.println("<BODY>");
out.println("<HTML>");
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s