Home arrow XML arrow Page 4 - XML Parsing With DOM and Xerces (part 2)

Data Overload - 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.

TABLE OF CONTENTS:
  1. XML Parsing With DOM and Xerces (part 2)
  2. The Writing On The Wall
  3. Highlights
  4. Data Overload
  5. Oops!
  6. Dear Diary
  7. Of Method And Madness
  8. Black Or White
  9. Link Out
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 9
March 06, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Let's take a closer look at the printData() function:

private void printData (NodeList NodeCollection) throws SAXException { try { if (NodeCollection != null) { for (int i=0; i< NodeCollection.getLength(); i++) { local = NodeCollection.item(i).getNodeName(); if(local.equals("item")) { // "item" element starts a new row out.write("<tr>"); getElementData(NodeCollection.item(i)); printData(NodeCollection.item(i).getChildNodes()); out.write("</tr>"); } else if( local.equals("name") || local.equals("supplier")) { // create table cells within row // align strings left out.write("<td><p align=left><font face=Verdana size=2>"); getElementData(NodeCollection.item(i)); printData(NodeCollection.item(i).getChildNodes()); out.write("</font></p></td>"); } else if( local.equals("id") || local.equals("cost") || local.equals("quantity")) { // create table cells within row // align numbers right out.write("<td><p align=right><font face=Verdana size=2>"); getElementData(NodeCollection.item(i)); printData(NodeCollection.item(i).getChildNodes()); out.write("</font></p></td>"); } } } } catch (IOException e) { throw new SAXException(e); } }
The printData() function maps different XML elements to appropriate HTML markup.

The different "item" elements correspond to rows within the table. The details of each item - name, supplier, quantity et al - are formatted as cells within each row of the table. Since Xerces represents this entire document as a tree, getting to each individual node involves moving between the different levels of the tree until a leaf, or terminal node, is encountered - which is why printData() utilizes the same recursive technique demonstrated in previous examples.

As stated earlier, for most of the elements, I'm simply displaying the content as is. The only deviation from this standard policy occurs with the "quantity" element, where an alert needs to be generated if the actual quantity falls below the minimum level. That's where the getElementData() function comes in - it contains code to test the current quantity against the minimum quantity, and highlight the data in red if the test fails. This function is quite complex, but the inline comments provided should help make it clearer.

private void getElementData(Node parentNode) throws SAXException { try { // get the type of the first child of the nodeset. int childType = parentNode.getFirstChild().getNodeType(); // only proceed further if it is a text node if (childType == Node.TEXT_NODE) { // get the value stored at the node String Content = parentNode.getFirstChild().getNodeValue(); // check for whitespace // proceed only if non-empty string if (!Content.trim().equals("")){ // check if node needs special handling (does the parent node has attributes?) if(parentNode.hasAttributes()) { // if parent has attributes, this one needs special handling // first get the attributes NamedNodeMap AttributesList = parentNode.getAttributes(); // iterate through the collection and get attribute details for(int j = 0; j < AttributesList.getLength(); j++) { // element-specific attribute handling if ( parentNode.getNodeName().equals("quantity") && AttributesList.item(j).getNodeName().equals("alert") ) { Quantity = new Integer(Content); Alert = new Integer(AttributesList.item(j).getNodeValue()); // if quantity lower than expected, highlight in red if(Quantity.intValue() < Alert.intValue()) { out.write("<font color="#ff0000">" + Quantity + "</font>"); } else { out.write("<font color="#000000">" + Quantity + "</font>"); } } else { // element has attributes, but no special treatment required out.write(Content); } } } else { // parent node has no attributes // just display the text out.write(Content); } } } } catch (IOException e) { throw new SAXException(e); } }
This function does a couple of different things. First, it checks to see if the node passed to it as an input parameter is a text node. If it is, the value of the node is checked, as is whether or not the parent node (the enclosing element) has any attributes. If no attributes are present and the node passes the whitespace test, the value is printed as is. In case the parent node *does* contain attributes, it must be either a "cost" or "quantity" element. If it's a "quantity" element, an additional test needs to be performed between the stated and alert quantities, with the resulting output highlighted on the basis of the test result.

With that out of the way, all that's left is to connect the class with the JSP page:

<%@ page language="java" import="java.io.IOException" %> <html> <head> </head> <body> <h1><font face=Verdana>Inventory Management</font></h1> <table width="55%" cellpadding="5" cellspacing="5" border="1"> <!-header row <tr> <td><p align=right><b><font face=Verdana size=2> Code</font></b></p></td> <td><b><font face=Verdana size=2>Name</font></b></td> <td><b><font face=Verdana size=2>Supplier</font></b></td> <td><p align=right><b><font face=Verdana size=2>Cost</font></b></p></td> <td><p align=right><font face=Verdana size=2><b>Quantity</b></font></p></td> </tr> <% try { MyFifthDomApp myFifthExample = new MyFifthDomApp("/www/xerces/WEB-INF/classes/inventory.xml",out); } catch (Exception e) { out.println("<font face="verdana" size="2">The following error occurred: <br><b>" + e + "</b></font>"); } %> </table> </body> </html>
Here's what it looks like:



 
 
>>> More XML Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: