Transforming HTML Form Data as XML Strings Using Java
A Refresher
We considered an XML document of the following format to be constructed from the data posted by the Web-based form:
<?xml version="1.0"?> <function> <name>ADD_USER</name> <parameters> <firstname>Jasmit</firstname> <middlename>Singh</middlename> <lastname>Kochhar</lastname> <contactinfo> <address>1234 Some Dr.</address> <city>Pleasant Hill</city> <state>CA</state> <zip>94523</zip> </contactinfo> </parameters> </function>
Any HTML-based form posts the data to a Web server in a URLEncoded format. Hence, if there is a name and value pair in the HTML form, the Web server receives this data in the following format:
URLEncoded(Name)=URLEncoded(Value)
If we have a variable with the following name and value pair:
Name = Variable/v1 and Value = Jasmit Singh
the corresponding name value pair posted to the Web server is
Variable%2Fv1=Jasmit+Singh
Here, both the name and the value are in the URLEncoded format. Hence, a set of name value pairs is represented in a Query String of the format, in which each name value is separated by &, as follows:
URLEncoded(Name1)=URLEncoded(Value1)&URLEncoded(Name2)=URLEncoded(Value2)&URLEncoded(Name3)=URLEncoded(Value3)
In a Java servlet, this Query String can be obtained by using the method getQueryString() of the request object. In case of a CGI script, the Query String is available as an environment variable called $QUERY_STRING.
For the purpose of this exercise, we will assume that you have written a small Java servlet or a CGI script that takes the Query String posted by the HTML form, passes it to the custom class that we create in this article, and obtains the XML document as an output string. It can then choose to save the string in a file or in a database. Our servlet will send it back to the user's browser as an XML string. The Web-based form has the following format:
<html> <head><title>New User Information</title></head> <body> <form action="TransformData" method="post" enctype="application/x-www-form-urlencoded"> <h2>Please provide the following information</h2> <input type="hidden" name="1/name" value="ADD_USER"> <table> <tr><td><b>First Name:</b></td> <td><input type="text" name="2../parameters/firstname" size="40"></td></tr> <tr><td><b>Middle Name:</b></td> <td><input type="text" name="3../middlename" size="40"></td></tr> <tr><td><b>Last Name:</b></td> <td><input type="text" name="4../lastname" size="40"></td></tr> <tr><td><b>Street Address:</b></td> <td><input type="text" name="5../contactinfo/address" size="40"></td></tr> <tr><td><b>City, State - Zip:</b></td> <td><input type="text" name="6../city" size="30">, <input type="text" name="7../state" size="2"> <input type="text" name="8../zip" size="10"></td></tr> <tr><td colspan="2"> <input type="submit" name="Submit" value="Submit"> </td></tr> </table> </form> </body></html>
The distinct features to note are the following:
TransformData is the name of the servlet that takes the Query String and calls the custom class to transform the data into XML string.
The field names start with the order in which we would like them to appear in the XML string.
The structure of the XML string is represented in the / notation for traversing a hierarchical tree. If one wants to start an element at the root node, we start the variable with a "/". If we want to process the element with the same parent as the current node, we represent that with "../". Hence, if we wanted to go two levels up, we would represent that as "../../".
Finally, any of the HTML form fields that are not to be included in the XML string are represented without any number appended to them. For example, the Submit button has no order appended to it.