Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed


email article
printer friendly

Mapping HTML to XML
Easily map form elements to XML nodes using XPath, HashMap, and dom4J
by Jeffrey Boltz

Posted March 17, 2004

On a recent project, I found myself with a restrictive set of requirements. I needed to read an XML document with no prior knowledge of the XML, and send it to a JavaServer Page (JSP) to be rendered. The resulting HTML page would be an input form that needed to post back to the servlet and update the original XML. This requirement restricted me from hard coding the form control name to the XML document.

The idea behind this concept is simple: I will explore using XPath as the form control name to map the form input data to update the XML document. The parameters from the form post are collected in a simple Java HashMap. From here, a simple algorithm spins through the collection and updates the XML document through random access. For instance, suppose that you want to read and manipulate this XML document:

<?xml version="1.0" 
  encoding="UTF-8"?>
<book>
  <title>The Illuminatus 
    Trilogy: The Eye in the 
    Pyramid, the Golden Apple 
    & Leviathan</title>
  <author>Robert Shea</author>
  <author>Robert Anton 
    Wilson</author>
  <isbn>0440539811</isbn>
</book>

I could read in the document using dom4j, for instance, and forward to the JSP to be rendered (see Figure 1). The sample.html file is shown in Listing 1.

ADVERTISEMENT

Notice that the name of the form element is a variation of an XPath—the "/" has been changed to "$." It was discovered that JavaScript does not like "/"; no worries, we just replace the "$? with a "/" when we need the real XPath. Remember, I wanted to get data from the form and process it without knowledge of the XML document or the HTML form in my application code. By using a HashMap, and XPath form element names, I can build a simple collection of name/value pairs to process later on. See Listing 2 for a method I came up with to do this.

Table 1 provides a visual of what the example in Listing 2 would render as a data structure. All I need to do is cycle through the Key/Value pairs and update the XML document. No a priori knowledge of the form or the XML is required.

This algorithm will work its way through the data structure and set each value based on its XPath from the control name set as the HashMap key. The form control value is the HashMap value (see Listing 3 for an example).

Several improvements still can be made to this approach. It was sort of my self-serving aim to put this out for criticism and review so that it can be improved upon and made useful to a broader base of programmers. See Table 2 for a list of improvements.

The code discussed here is hardly rocket science. Indeed, more than anything what I've illustrated is the power of XML (both with dom4J and Xpath) and basic data structures and algorithms.

About the Author
Jeff Boltz is a Java programmer who works with J2EE and XML. Contact Jeff at jeffrey.boltz@bbh.com.




Back to top












Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTPOnline Home