HomeXML Page 6 - XML Parsing With DOM and Xerces (part 2)
Dear Diary - XML
In this concluding article of a two-part series, use your knowledge of DOM processing with Xerces to construct simple Web applications based on Xerces, XML and JSP. Examples include transforming an XML file to HTML via Xerces and dynamically generating an XML document tree from a MySQL database.
Let's try one more example, this one demonstrating an alternative technique of formatting an XML document into HTML Here's the XML file:
<?xml version="1.0"?>
<todo>
<item>
<priority>1</priority>
<task>Figure out how Xerces works</task>
<due>2001-12-12</due>
</item>
<item>
<priority>2</priority>
<task>Conquer
the last Quake map</task>
<due>2001-12-31</due>
</item>
<item>
<priority>3</priority>
<task>Buy a Ferrari</task>
<due>2005-12-31</due>
</item>
<item>
<priority>1</priority>
<task>File tax return</task>
<due>2002-03-31</due>
</item>
<item>
<priority>3</priority>
<task>Learn
to cook</task>
<due>2002-06-30</due>
</item>
</todo>
Here's what it should look like:
As with the previous examples, this has two components: the source code for the Java class, and the JSP page which uses the class. Here's the class:
import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.SAXException;
import
org.w3c.dom.*;
import java.util.*;
import java.io.*;
public class MySixthDomApp
{
private Writer out;
private String local = "";
// define a hash
table
to store HTML markup
// this hash table is used in the callback functions
// for start, end and character elements ("priority" only)
private Map StartElementHTML
= new HashMap();
private Map EndElementHTML = new HashMap();
private Map
PriorityHTML = new HashMap();
// constructor
public MySixthDomApp
(String xmlFile, Writer out) throws SAXException
{
this.out = out;
// initialize StartElementHTML Hashmap
StartElementHTML.put("todo","<ol>n");
StartElementHTML.put("item","<li>");
StartElementHTML.put("task","<b>");
StartElementHTML.put("due"," <i>(");
// initialize
EndElementHTML Hashmap
EndElementHTML.put("todo","</ol>n");
EndElementHTML.put("item","</font></li>n");
EndElementHTML.put("task","</b>");
EndElementHTML.put("due",")</i>");
// initialize PriorityHTML
Hashmap
PriorityHTML.put("1","<font face="Verdana" color="#ff0000"
size="2">");
PriorityHTML.put("2","<font face="Verdana" color="#0000ff"
size="2">");
PriorityHTML.put("3","<font face="Verdana" color="#000000"
size="2">");
// create a Xerces DOM parser
DOMParser parser = new
DOMParser();
// parse the document
try {
parser.parse(xmlFile);
Document document = parser.getDocument();
Element
RootElement
= document.getDocumentElement();
NodeList Children =
RootElement.getChildNodes();
printData(Children);
out.flush();
} catch
(IOException e) {
throw new SAXException(e);
}
}
private void printData (NodeList NodeCollection)
throws SAXException
{
try {
// check if the node collection
passed is NULL
if (NodeCollection != null) {
// iterate
through the
collection
for (int i=0; i< NodeCollection.getLength();
i++)
{
// store the name of the element in a string.
// this is used as a key into the HashMap.
local
= NodeCollection.item(i).getNodeName();
if(NodeCollection.item(i).getNodeType()
==
Node.ELEMENT_NODE)
{
// "priority" element needs special
handling
// for everything else...
if(!((local.equals("priority"))))
{
// get the HTML
markup
String
StartElement =
StartElementHTML.get(local).toString();
String EndElement =
EndElementHTML.get(local).toString();
// output starting HTML tags + content
out.write(StartElement
+
GetElementData(NodeCollection.item(i)));
// recursively
traverse the tree
printData(NodeCollection.item(i).getChildNodes());
// output ending HTML tags
out.write(EndElement);
} else {
// handle the "priority"
element differently.
// get the data for the "priority"
element
String Priority =
GetElementData(NodeCollection.item(i));
// use "priority" element value to get markup for
the
rest
of the line
out.write((PriorityHTML.get(Priority)).toString());
// move on
printData(NodeCollection.item(i).getChildNodes());
}
}
}
}
} catch (IOException e) {
throw new SAXException(e);
}
}
private String GetElementData(Node parentNode) {
// get node type
int childType = parentNode.getFirstChild().getNodeType();
// return the node value if text node
if (childType == Node.TEXT_NODE)
{
if(parentNode.getFirstChild().getNodeValue() != null) {
return parentNode.getFirstChild().getNodeValue();
}
}
// else return null
return null;
}
}
This is much cleaner and easier to read than the previous example, since it uses
Java's HashMap object to store key-value pairs mapping HTML markup to XML markup. Three HashMap's have been used here: StartElementHTML, which stores the HTML tags for opening XML elements; EndElementHTML, which stores the HTML tags for closing XML elements; and PriorityHTML, which stores the HTML tags for the "priority" elements defined for each "item".
These HashMaps are populated with data in the class constructor:
The rest of the constructor follows the pattern set previously - initialize parser,
parse XML, get document element and first level children, and hand processing over to printData().
The printData() function has also been modified to incorporate the HashMaps described above. Every time the function encounters an XML element, it uses the element name as a key into the HashMaps, retrieves the corresponding HTML markup for that element, and prints it. If the element is a "priority" element, there's no real data to be printed; rather, the formatting of the entire to-do item changes to reflect the item priority.
private void printData (NodeList NodeCollection) throws SAXException {
try {
// check if the node collection passed is NULL
if (NodeCollection
!= null) {
// iterate through the collection
for (int
i=0; i< NodeCollection.getLength(); i++) {
// store the name
of the element in a string.
// this is used as a key into the
HashMap.
local = NodeCollection.item(i).getNodeName();
if(NodeCollection.item(i).getNodeType()
==
Node.ELEMENT_NODE) {
// "priority" element needs special
handling
// for everything else...
if(!((local.equals("priority"))))
{
// get the HTML markup
String StartElement
=
StartElementHTML.get(local).toString();
String EndElement
=
EndElementHTML.get(local).toString();
// output starting
HTML tags + content
out.write(StartElement +
GetElementData(NodeCollection.item(i)));
// recursively traverse the tree
printData(NodeCollection.item(i).getChildNodes());
// output ending HTML tags
out.write(EndElement);
} else {
// handle the "priority" element
differently.
// get the data for the "priority" element
String Priority =
GetElementData(NodeCollection.item(i));
// use "priority" element value to get markup for the
rest
of the line
out.write((PriorityHTML.get(Priority)).toString());
// move on
printData(NodeCollection.item(i).getChildNodes());
}
}
}
}
} catch (IOException
e) {
throw new SAXException(e);
}
}
Here's the JSP code that uses the class above:
<%@ page language="java" import="java.io.IOException" %>
<html>
<head>
</head>
<body>
<h1><font
face="Verdana">My Todo List</font></h1>
<%
try {
MySixthDomApp
mySixthExample = new
MySixthDomApp("/www/xerces/WEB-INF/classes/todo.xml ",out);
} catch (Exception e) {
out.println("<font face="verdana" size="2">Something
bad just
happened: <br><b>" + e + "</b></font>");
}
%>
</body>
</html>