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:
Trilogy: The Eye in the
Pyramid, the Golden Apple
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.
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 firstname.lastname@example.org.
Back to top