Logo         Community
The Company
Your Account
Contact Us
XML Parsing With DOM and Xerces (part 2)
Use your knowledge of DOM processing with Xerces to construct simple Web applications based on Xerces, XML and JSP

| The Writing On The Wall |

Let's begin by quickly revisiting the sample XML file used earlier:

<?xml version="1.0"?>
  <!-- time to lock and load -->
      <name>Rusty, jagged nails for nailgun</name>
      <supplier>NailBarn, Inc.</supplier>
      <cost currency="USD">2.99</cost>
      <quantity alert="500">10000</quantity>
      <name>Power pack for death ray</name>
      <cost currency="USD">9.99</cost>
      <quantity alert="20">10</quantity>

This next example ports the dynamically-printed tree structure you saw in a previous example to work with a Web server.

import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.SAXException;
import org.w3c.dom.*;
import java.io.*;

public class MyFourthDomApp {

  private Writer out;
  String Content = "";
  // a counter for keeping track of the "tabs"
  private int TabCounter = 0;
  // constructor
  public MyFourthDomApp (String xmlFile, Writer out) {
      this.out = out;

      //  create a Xerces DOM parser
      DOMParser parser = new DOMParser();
      //  parse the document and
      // access the root node with its children.
      try {
            Document document = parser.getDocument();
            } catch (SAXException e) {
                  // something went wrong!
            } catch (IOException e) {
                  // something went wrong!
  // recursively traverse the document tree
  private void NodeDetails (Node node) {
      try {
        int type = node.getNodeType();

        if (type == Node.ELEMENT_NODE) {
            // if element
            out.write ("Element: " + node.getNodeName() + "<br>");
            if(node.hasAttributes()) {
              NamedNodeMap AttributesList = node.getAttributes();
              for(int j = 0; j < AttributesList.getLength(); j++) {
                  out.write("Attribute: " +
AttributesList.item(j).getNodeName() + " = " +
AttributesList.item(j).getNodeValue() + "<br>");
        } else if (type == Node.TEXT_NODE) {
            // if character data
            Content = node.getNodeValue();
                    if (!Content.trim().equals("")){
                  out.write ("Character Data: " + Content + "<br>");
        } else if (type == Node.COMMENT_NODE) {
            // if comment
            Content = node.getNodeValue();
                  if (!Content.trim().equals("")){
                  out.write("Comment: " + Content + "<br>");
            // add code for other node types here if you like     
        NodeList children = node.getChildNodes();
        if (children != null) {
            for (int i=0; i< children.getLength(); i++) {

      } catch (IOException e) { 
        // something went wrong!
  // this formats the output as a tree
  private void FormatTree (int TabCounter) {
      try {
        for(int j = 1; j < TabCounter; j++) {
      } catch (IOException e) { 
        // something went wrong!

The most important difference between this example and the previous one is the introduction of a new Writer object, which makes it possible to redirect output to the browser instead of the standard output device.

private Writer out;

The constructor also needs to be modified to accept two parameters: the name of the XML file, and a reference to the Writer object.

// constructor
public MyFourthDomApp (String xmlFile, Writer out) {

  // snip!

Note that, since I'm using a Writer object, I also need to add some code to trap and resolve IOExceptions, if they occur. I've left that aside for a more detailed discussion a little later - however, you should be aware of this requirement and design your code appropriately.

You'll notice that I haven't made any major changes to the NodeDetails() function, other than the inclusion of HTML tags in the output - this is what will ultimately get sent to the browser. The only item left is to handle the connection between the Java class above and the Web server - which is where JSP comes in.

Here's the JSP page that brings it all together:

<%@ page language="java" import="java.io.IOException" %>
  try {
        MyFourthDomApp myFourthExample = new
  } catch (Exception e) {
        out.println("Something bad happened!" + e);


And here's what the output looks like:

Output image

For the moment, I'll ignore any errors that may occur when processing the JSP page, deferring this to a later discussion.

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback