|
Java™ by example!
|
|
|
How do I create 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:
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:\testing\xml\TestTemplate.html:
To generate the .class file for this 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:
Compile and run. Result:
For more information, check the tutorial at http://www.pisoftware.com/publications/xmlc-tutorial/intro.html.
Further Information
Author of answer: Joris Van den Bogaert
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|