Sun Java Solaris Communities My SDN Account Join SDN
 
Code sample

Java API for XML Processing API (JAXP) Using DOM

 
Function
The Java API for XML Processing API (JAXP) enables applications to parse and transform XML documents using an API that is independent of a particular XML processor implementation. The Document Object Model (DOM) is available as part of the Java API for XML Processing (JAXP). Along with Simple API for XML (SAX), DOM is one of two common ways to write software that accesses XML data.
 
Using DOM with JAXP, developers can create a representation of an XML document in runtime memory either by parsing the data from an existing XML source or constructing the document using the DOM API. Once the document exists in memory, it is possible to access or modify any element in random order.
 
For very large sets of XML data, using DOM may not be practical because it may demand too much runtime memory. In such cases developers should use SAX, which gives more constrained access to the XML data but is more efficient.
 
Topics Covered
This sample performs several functions common to software used to process orders represented in XML format. After constructing an order in memory, it counts the total number of items in the order, applies a volume discount, and calculates sales tax.
 
It uses the standard Java API for XML available with the Java 2 Platform Software Developer Kit (J2SDK), version 1.4. The code in this sample illustrates the following JAXP-DOM concepts:
  • Constructing an XML document from scratch in memory using DOM
  • Converting an XML document in the file system to a DOM object in memory
  • Converting a DOM object in memory into an XML document in the file system
  • Accessing and manipulating the contents of the DOM object in memory
 

Back to top

Requirements
This sample requires the following products:
 

Back to top

Source Files
Download the Java source code files or click on the links below to view individual files.
 
DOMSample.java
 
This is the driver program for the application. It uses the classes described below to process the order.
 
CreateOrderDOM.java
 
This class constructs an XML document in memory using DOM. It first creates the root Order element and subsequently creates components of the order by inserting nodes to the root element.
 
DOMUtil.java
 
This utility class contains static methods to:
  • Write a DOM document to a file
  • Recursively print a specified node, and then print all of its children
  • Parse the XML file and create a DOM object in memory
  • Count elements in a document by tag name
 
Order.java
 
This class implements the business logic for the sample. Its public methods perform the following:
  • getPrice: Returns order price without tax.
  • getTax: Returns order tax.
  • getOrderPrice: Returns order total.
  • isDiscount: Returns true if order price is greater the discount limit.
  • applyDiscount: Loops through all item prices and applies discount to each.
  • addTaxAndTotal: Creates and appends TAX and TOTAL elements.

Its private methods perform the following:

  • calculateOrder: Returns sum of orderTax and orderPrice.
  • calculateTax: Calculates the order tax and returns it.
  • countPrice: Calculates the order price without taxes by loopiping through all prices.
  • reCalculateOrder: Calculates the order total.

 
Order.xml
 
Order data in XML format.
 

Back to top

Result Output

Back to top

Javadoc

Back to top

Instructions
Follow these steps to run the sample:
  • Make sure you have the J2SE SDK, version 1.4 installed on your system. This sample will not work with earlier versions of the J2SDK.
  • Download all Java source code files into any directory on your system.
  • From the directory you created, compile all classes with the command:

    javac *.java

  • Run the sample by invoking the command:

    java DOMSample

  • Compare the output statements from the execution with the source code of the Java files and the XML file generated to see how the various functions were implemented.
 

Back to top

See Also
JAX API for XML Processing (JAXP) Documentation
(http://java.sun.com/xml/jaxp/docs.html)
 

Back to top

Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.

Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.