} }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:
- javax.xml.stream.XMLInputFactory class
- javax.xml.stream.XMLStreamException exception
- javax.xml.stream.XMLStreamReader interface
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:
- getStreamReader(): Creates an XML stream reader from the order.xml file using the XMLInputFactory.newInstance().createXMLStreamReader() method.
- parseOrder(): Parses the XML document order.xml. The method first creates a new OrderBO business object that will contain the parse order. The OrderBO and other business objects are described later in this section. Then the method uses the toStartTag() method to get a reference to the first start element of the stream. The method then uses the XMLStreamReader.next() method to search the input stream for customer and item list information, using the OrderConstants CUSTOMER and ITEM_LIST, respectively. Once it finds one of the elements, it uses the parseCustomer() and parseItem() methods to process the respective piece of information and create CustomerBO or ItemBO business objects.
- parseCustomer(): Instantiates a CustomerBO business object, then uses the parseName() and parseAddress() methods to set the name and address of the CustomerBO object. It then passes back the CustomerBO object to the parseOrder() method to add to the OrderBO object.
- parseName(): Uses the XMLStreamReader.next() method to search the input stream for first and last names, using the OrderConstants FIRST_NAME and LAST_NAME. Once the method finds the names, it uses the setFirstName() and setLastName() methods of the CustomerBO class to set the names of the instantiated object.
- parseAddress(): Uses the XMLStreamReader.next() method to search the input stream for the address of the customer, using the OrderConstants ADDRESS, STREET, CITY, STATE and ZIP. Once the method finds the address, it uses the setStree(), setCity(), setState(), and setZip() methods of the CustomerBO class to set the address of the instantiated object.
- parseItem(): Instantiates an ItemBO business object, then uses the XMLStreamReader.next() method to search the input stream for information about an item, using the OrderConstants ITEM_NUMBER, DESCRIPTION, QUANTITY, and PRICE. Once the method finds the item information, it uses the setItemNumber(), setDescription(), setQuantity, and setPrice() methods of the ItemBO class to set the item information of the instantiated object, and then passes back the ItemBO object to the parseOrder() method to add to the OrderBO object.
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: Contains the order number.
- customerBO: Contains a CustomerBO object.
- items: 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.
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:
- Install Oracle WebLogic Server, including the examples.
- Start the Examples server.
- Set up your environment.
Configure Oracle WebLogic Server
No special configuration is required for this example
Build and Deploy the Example:
- 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.
- 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:
- Complete the steps in the "Prepare the Example" section.
- 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.