Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

Transforming HTML Form Data as XML Strings Using Java

  • Print
  • + Share This
In a previous article, "Posting HTML Form Data as XML Strings," Jasmit Kochhar demonstrated a technique to transform data posted by an HTML form into a well-formed XML document using Active Server Pages (ASP). In this article, he shows you how to create a Java class that does a similar transformation. The class may be called from a Java servlet or a CGI script to suitably format the data.
Like this article? We recommend

Like this article? We recommend

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"?>
   <address>1234 Some Dr.</address>
    <city>Pleasant Hill</city>

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:


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


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:


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:

<head><title>New User Information</title></head>

<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">

 <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">


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.

  • + Share This
  • 🔖 Save To Your Account