Package examples.xml.orderParser

This example illustrates how to use the WebLogic XML Streaming API to parse an XML document and bind its elements to Java business objects.

See:

 

Class Summary

Client Initiates the parsing of an XML document into business objects using the XMLInputStream API.
CustomerBO Data representation of a customer.
ItemBO Data representation of an item.
OrderBO Data representation of an order.
OrderConstants Constants used while parsing an order XML document.
StreamParser Demonstrates the parsing of an XML document into business objects using the XMLInputStream API.
 

Package examples.xml.orderParser Description

This example illustrates how to use the WebLogic XML Streaming API to parse an XML document and bind its elements to Java business objects.

The client application that drives the example is called Client. This simple Java application takes a single parameter: an XML file that describes an order. In the example, this file is called order.xml. The Client application then creates a StreamParser object which does most of the work of the example, such as parsing the XML file and populating business objects. Once all the processing is complete, the Client application prints out the resulting business object.

The StreamParser class contains the following methods, mostly called in the order shown:

  • getInputStream(): creates an XML input stream from the order.xml file using the XMLInputStreamFactory.newInputStream() method.

  • parse(): parses the XML document order.xml. This method first uses the XMLInputStream.skip(), XMLInputStream.next(), and XMLInputStream.getName().getLocalName() methods to ensure that the root element of the order XML file is <order>. If it is, then the parse() method creates a new OrderBO business object that will contain the order. The OrderBO and other business objects are described later in this section.

    The method then uses the XMLInputStream.skip(), XMLInputStream.peek(), and XMLInputStream.getName().getLocalName() methods to search the input stream for customer and itemlist information, described by the <customer> and <itemlist> elements respectively. Once it finds one of the elements, it uses the XMLInputStream.getSubstream() method to get the respective child element (either <customer> or <itemlist>), then calls either the processCustomer() or processItemList() methods to process the substream and create CustomerBO or ItemBO business objects.

  • processCustomer(): this method first instantiates a CustomerBO business object, then iterates through the <customer> substream using the XMLInputStream.skip(), XMLInputSream.peek(), and XMLInputStream.next() methods, searching for known customer element names to populate the CustomerBO business object. The method uses the CustomerBO.setXXX() methods to populate each field (firstName, lastName, and so on) of the customer business object.

  • processItemList(): iterates over the <itemlist> substream, looking for <item> elements. When it finds one, it uses the XMLInputStream.getSubStream() method to get the <item> substream and passes it to the processItem() method for further processing.

  • processItem(): this method first instantiates a ItemBO business object, then iterates through the <item> substream using the XMLInputStream.skip() and XMLInputSream.peek() methods, searching for known item elements to populate the ItemBO business object. The method uses the ItemBO.setXXX() methods to populate each field (description, quantity, and so on) of the item business object. Finally, once the item has been processed, the ItemBO business object is added to the current Vector of items associated with the current OrderBO.

  • getAttribute(): helper method that gets the value of a known attribute of an element based on the attribute's name. The method first creates an AttributeIterator object that contains all the attributes of a given element, then iterates over the set of attributes using the AttributeIterator.hasNext() and AttributeIterator.next() methods, searching for a particular attribute based on the name passed to the getAttribute() method.

  • getOrderBO(): prints out the OrderBO business object after the order.xml file has been fully parsed and processed.

The OrderBO business object describes an order. It contains the following fields, along with respective getter and setter methods to get and set the values:

  • number that contains the order number.
  • customerBO that contains a CustomerBO object.
  • items that contains a Vector of ItemBO objects.
The OrderBO business object also has a printOrder method to print out the order once the order XML file has been fully processed. The CustomerBO business object describes a customer. It contains the following String fields, along with respective getter and setter methods to get and set the values:
  • firstName
  • lastName
  • street
  • city
  • state
  • zip
The ItemBO business object describes an item in an item list. It contains the following fields, along with the respective getter and setter methods get and set the values:
  • number
  • description
  • quantity
  • price
Finally, the OrderConstants class contains the list of element names that appear in the order.xml file, such as <order>, <customer>, <itemlist>, and so on. This class is used in the StreamParser class when iterating over the order.xml file, looking for known elements.

 

  Additional Resources for examples.xml.orderParser

order.xml XML document to be parsed containing a single order.
build.xml The Java Ant build script compiles the example.

 

The following sections describe how to build and run the example.

  1. Prerequisites
  2. Build the example
  3. Run the example from the commandline
  4. Check the output
  5. Deploy the example

 

Prerequisites

Before you run this example, you need:

  • WebLogic Server Version 8.1

 

Build the Example

To build the example, follow these steps:
  1. Set up your development shell as described in Quick Start.

  2. Go to

    $SAMPLES_HOME/server/examples/src/examples/xml/orderParser

  3. Assemble and compile the example by executing the Java ant utility at the command line:
    prompt> ant

    The ant utility uses the build.xml build script, which compiles the OrderConstants, OrderBO, CustomerBO, ItemBO, StreamParser, and Client Java source files.

 

Run the Example

  1. Start the examples WebLogic Server.

  2. In your development shell, run the Client Java application using the following command:
    prompt> ant run 

 

Check the Output

You should see the following output if the example runs successfully:
 
Printing contents of OrderBO...
 
Order Number:
  1234
Customer Info:
  John Doe
  2000 Pine St
  San Francisco, CA 94403
Order Info:
  4321 Stuffed Pastry 10x2.95
  8765 Chocolate 5x9.5
  9922 Cookie 30x0.85
 
Successfully handled the document

 

Deploy the example

Deploy the example to the console...

www.setgetweb.com:7001/console ***

...as part of the WebLogic Server Examples application...

http://www.setgetweb.com:7001/examplesWebApp/index.jsp ***

 

See Also

Read more about: