Creating an HTML page from an XML template using XMLC

Enhydra’s XMLC allows you to have random access to your XML document by compiling the XML document into a Java class that contains a DOM (document object model) representation. For every XML template that you provide, a Java class equivalent is generated. Using this class, you can not only access your tags with standard DOM methods, but XMLC also provides convenience methods like getters and setters to dynamically alter the elements.

So, with XMLC, you don’t have to include Java code inside your HTML (JSP) nor vice versa (servlets).

For every tag that contains a ID attribute, a getElementXXX() is generated. For example, if you have the following tag defined in your template:

   <font id="topcolor" color="">test color</font>

The method getElementTopcolor() is generated which returns an object of type org.w3c.dom.html.HTMLFontElement.

It also creates setTextXXX methods for your ID elements, so you can easily change the text between tags.

The following is a simple example that starts from a template HTML, dynamically modifies its element and prints out the resulting HTML.

First follow the instructions on this page to download and install XMLC.

I created the file C:testingxmlTestTemplate.html:

<html>
<head>
   <title id="title">test title</title>
</head>
<body>
   <h1 id="topheader">test header</h1>
   <font id="topcolor" color="">test color</font>
   <br>
   <br>
   <span id="maintext">test span</span>
</body>
</html>

To generate the .class file for this HTML:

Enhydra$ //c/javalibs/xmlc2.0.1/bin/xmlc -keep TestTemplate.html

(Note 1: I installed xmlc in c:javalibs; Note 2: -keep specifies that the .java file should not be deleted after generation. This way, you can inspect the .java file and find out what methods are available!)

The “client”: Main.java:

import org.w3c.dom.html.*;
import org.w3c.dom.*;
 
public class Main
{
   public static void main(String []args) {
      TestTemplate tt = new TestTemplate();
 
      // get title and change it 
      HTMLTitleElement title = tt.getElementTitle();
      title.setText("Generated XML");
 
      // set element with id "topheader"
      tt.setTextTopheader("Insightful quote");
  
      // set element with it "topcolor"
      tt.setTextTopcolor("[I cannot be held responsible]");
      HTMLFontElement elem = tt.getElementTopcolor();
      elem.setColor("#ff0000");
 
      // set element with id "maintext"
      tt.setTextMaintext("The shorter you live, the longer you're dead");
 
      // generate new HTML
      System.out.print(tt.toDocument());
   }
}

Compile and run. Result:

<HTML>
<HEAD>
   <TITLE id="title">Generated XML</TITLE>
</HEAD>
<BODY>
   <H1 id="topheader">Insightful quote</H1>
   <FONT color="#ff0000" id="topcolor">[I cannot be held responsible]</FONT> 
   <BR> 
   <BR> 
   <SPAN>The shorter you live, the longer you're dead</SPAN>
</BODY>
</HTML>

For more information, check the tutorial at http://www.pisoftware.com/publications/xmlc-tutorial/intro.html.