Sun Java System logo     
XML DOM Sample Application



XML DOM Sample Application


This document describes how to utilize the XML DOM sample application in conjunction with Sun Java (tm) System Application Server version 7.

This sample application document contains the following sections:



Overview

In this section, the XML DOM sample application is described.

The XML DOM example reads an XML file and generates a DOM Document object from it. The steps to do this are as follows:

  1. Create a DocumentBuilderFactory and setNamespaceAware as follows:

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);

  2. Create a DocumentBuilder as follows:

    DocumentBuilder db = dbf.newDocumentBuilder();

  3. Parse the input file to get a Document object as follows:

    Document doc = db.parse(new File(filename);

The program also gives an example of using an error handler . Finally, this program helps you understand how DOM works by showing you the structure and contents of a DOM tree.



Compiling and Assembling the Application



To build the entire application from scratch, follow these steps: .

  1. Go to sjsas_install_dir/samples/xml/dom/src

    Execute the command asant

    The default target core will be executed to compile the application.

  2. Clean the application project area (optional).

    Execute the command asant clean to remove the sample application assemble and build directories.



Running the Sample Application

To run the sample application, do the following:

Go to sjsas_install_dir/samples/xml/dom/src and execute the following command:

asant run

If your deployment process was successful, the sample displays the following information:

[java] DOC: nodeName="#document"

[java] ELEM: nodeName="examples" local="examples"

[java] TEXT: nodeName="#text" nodeValue=[WS]

[java] ELEM: nodeName="section" uri="http://www.example.com/books-r-us" local="section"

[java] ATTR: nodeName="xmlns" uri="http://www.w3.org/2000/xmlns/" local="xmlns" nodeValue="http://www.example.com/books-r-us"

[java] TEXT: nodeName="#text" nodeValue=[WS]

[java] ELEM: nodeName="title" uri="http://www.example.com/books-r-us" local="title"

[java] TEXT: nodeName="#text" nodeValue="Book-Signing Event"

[java] TEXT: nodeName="#text" nodeValue=[WS]

[java] ELEM: nodeName="signing" uri="http://www.example.com/books-r-us" local="signing"

[java] TEXT: nodeName="#text" nodeValue=[WS]

[java] ELEM: nodeName="author" uri="http://www.example.com/books-r-us" local="author"

[java] ATTR: nodeName="title" local="title" nodeValue="Mr."

[java] ATTR: nodeName="name" local="name" nodeValue="Vikram Seth"

[java] TEXT: nodeName="#text" nodeValue=[WS]

[java] ELEM: nodeName="book" uri="http://www.example.com/books-r-us" local="book"

[java] ATTR: nodeName="title" local="title" nodeValue="A Suitable Boy"

[java] ATTR: nodeName="price" local="price" nodeValue="$2295"

[java] TEXT: nodeName="#text" nodeValue=[WS]

[java] TEXT: nodeName="#text" nodeValue=[WS]

[java] TEXT: nodeName="#text" nodeValue=[WS]

[java] ELEM: nodeName="reservation" local="reservation"

[java] ATTR: nodeName="xmlns:html" uri="http://www.w3.org/2000/xmlns/" pre="xmlns" local="html" nodeValue="http://www.w3.org/TR/REC-html40"

[java] TEXT: nodeName="#text" nodeValue=[WS]

[java] ELEM: nodeName="name" local="name"

[java] ATTR: nodeName="html:class" uri="http://www.w3.org/TR/REC-html40" pre="html" local="class" nodeValue="largeSansSerif"

[java] TEXT: nodeName="#text" nodeValue="Seth, Vikram"

[java] TEXT: nodeName="#text" nodeValue=[WS]

[java] ELEM: nodeName="seat" local="seat"

[java] ATTR: nodeName="class" local="class" nodeValue="Y"

[java] ATTR: nodeName="html:class" uri="http://www.w3.org/TR/REC-html40" pre="html" local="class" nodeValue="largeMonotype"

[java] TEXT: nodeName="#text" nodeValue="33B"

[java] TEXT: nodeName="#text" nodeValue=[WS]

[java] ELEM: nodeName="html:a" uri="http://www.w3.org/TR/REC-html40" pre="html" local="a"

[java] ATTR: nodeName="href" local="href" nodeValue="/servlets/ResStatus"

[java] TEXT: nodeName="#text" nodeValue="Check Status"

[java] TEXT: nodeName="#text" nodeValue=[WS]

[java] ELEM: nodeName="departure" local="departure"

[java] TEXT: nodeName="#text" nodeValue="1997-05-24T19:22:00+5"

[java] TEXT: nodeName="#text" nodeValue=[WS]

[java] TEXT: nodeName="#text" nodeValue=[WS]


Generating Javadocs

To understand sample application source code better, you can refer to the related javadocs. To generate javadocs, run the command

asant javadocs

or

asant all

After javadocs are generated, you can access them at

<i nstall_dir>/samples/xml/dom/javadocs/index.html



Troubleshooting



  • If you re unable to compile the application, check whether you have asant in your PATH.

  • If you don't receive the required results, or you receive an error, check whether you have xml data files under sjsas_install_dir/samples/xml/data directory.



Copyright © 2002 Sun Microsystems, Inc. All rights reserved.

Last Updated July 12, 2002