|
Generating Dynamic Text Content
The first version of StockQuotes.html sets the stage for adding dynamically generated text for a customer's name and the current time. The following HTML markup contains two SPAN tags with ID attributes "custName" and "reportTime." The XMLC-generated Java class for this markup contains the convenience methods setTextCustName(String) and setTextReportTime(String) that provide the easy access points for setting the Web page content.
<! -- StockQuotes.html, Version 1 -->
<HTML>
<TITLE>
Stock Quotes
</TITLE>
<BODY BGCOLOR="#D0D0D0">
<H2 align=CENTER>
Welcome < span id="custName"> Customer Name< /span>
</H2>
<H3>
Stock quotes for < span id="reportTime"> current time< /span>:
</H3>
</BODY>
</HTML>
The following command uses XMLC to actually generate the Java class for the above markup.
xmlc StockQuotes.html
XMLC reads the input file "StockQuotes.html" and generates a Java class file named "StockQuotes.class." The command line argument '-keep' may be used to instruct XMLC to keep an intermediary Java source file, "StockQuotes.java," which the compiler deletes by default. When first learning XMLC, however, poking around in that file is quite instructive, but not strictly necessary.
| |
 |
Figure 1 | Customer "Elmer Fudd"
|
Now that I have the class "StockQuotes," I create DynStockQuotes to actually perform Web page manipulation utilizing that class. Typically, the code in "DynStockQuotes" (sans the main method) would be placed in a file operating within a servlet architecture. I'm using the code free of servlets for ease of demonstration. The method DynStockQuotes.main creates a dynamic StockQuotes page and prints the generated HTML code for the specified customer name to the standard output stream. The image to the right shows the resulting Web page for customer "Elmer Fudd."
The setting of dynamic content in class DynStockQuotes occurs inside the method
createHtml(String):
StockQuotes stockQuotes = new StockQuotes();
stockQuotes.setTextCustName( custName );
stockQuotes.setTextReportTime( new Date().toString() );
return stockQuotes.toDocument();
A newly created StockQuotes object is used to set the text for customer name and report time through convenience methods provided by XMLC. The toDocument() method renders the StockQuotes DOM into HTML as a String.
That's all it takes to generate dynamic content! XMLC has done all the dirty work. With just a few lines of code, I use an XMLC generated class to alter the Web page content. And best of all, my HTML file doesn't contain any Java code or non-standard HTML markup.
|