} }

 Files   Prepare   Run   Troubleshooting   Related Topics 

 

About the Example

This example illustrates how to use the Streaming API for XML (StAX) to parse an XML document and bind its elements to Java business objects. StAX is specified by JSR-173 in the Java Community's Java Technology Specifications. The StAX API provides some new elements that are implemented in the example (in StreamParser.java) and that are fully documented here:

The example is driven by a client application 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:

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:

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:

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:

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.


 

Files Used in the Example

Directory Location:

MW_HOME/wlserver_10.3/samples/server/examples/src/examples/xml/stax/

(where MW_HOME is the directory containing your Oracle WebLogic Server installation)

File

Click source files to view code.

Description

order.xml XML document to be parsed containing a single order.
build.xml Ant build script that compiles the example.
Client.java Client application that instantiates the parsing of an XML document into business objects using the XMLStreamReader StAX API.
CustomerBO.java Java representation of a Customer. Includes fields such as first name, last name, street, and so on.
ItemBO.java Java representation of an Item. Includes fields such as number, description, quantity, and price.
OrderBO.java Java representation of an Order. An order contains a single customer, represented by the CustomerBO object, and a vector of items, representated by the ItemBO objects.
OrderConstants.java Java file that lists the constants used in the parseXXX() methods when parsing an order.
StreamParserClient.jsp StreamParser.jsp is the example's client which reads in the given XML file, parses it using StreamParser, and prints the contents in an HTML table.
StreamParser.java Main class that calls the various parseXXX() methods to parse an XML document that represents an order.

 

Prepare the Example

 

Prerequisites

Before working with this example:

  1. Install Oracle WebLogic Server, including the examples.
  2. Start the Examples server.
  3. Set up your environment.

 

Configure Oracle WebLogic Server

  No special configuration is required for this example

 

Build and Deploy the Example:

  1. Change to the SAMPLES_HOME\server\examples\src\examples\xml\stax directory, where SAMPLES_HOME refers to the main Oracle WebLogic Server examples directory, such as d:\beahome\wlserver_10.3\samples.
  2. Execute the following command from the shell where you set your environment:

    ant build

    This command compiles and stages the example.

 

Run the Example

To run the example, follow these steps:

  1. Complete the steps in the "Prepare the Example" section.
  2. In your development shell, run the example by using the following command from the example directory (SAMPLES_HOME\server\examples\src\examples\xml\stax\):

    prompt> ant run

Check the Output

You should see the following output if the example runs successfully:

Buildfile: build.xml

run: [java] Printing contents of OrderBO... [java] Order Number: 1234 [java] Customer: [java] John Doe [java] 2000 Pine St [java] San Francisco, CA 94403 [java] Item: [java] 4321 Stuffed Pastry, 10 @ $2.95 per [java] Item: [java] 8765 Chocolate, 5 @ $9.5 per [java] Item: [java] 9922 Cookie, 30 @ $0.85 per
[java] Successfully handled the document
BUILD SUCCESSFUL
Total time: 0 seconds

 

Troubleshooting


 

Related Topics

(Internet connection required.)


Copyright © 1996, 2009, Oracle and/or its affiliates. All Rights Reserved.