Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Use the Best of StAX and XMLBeans to Stream XML Object Binding

Learn how to combine the StAX and XMLBeans APIs to leverage the benefits of each: StAX to parse and chunk incoming XML into a manageable size and XMLBeans to load the chunks into objects for processing.


advertisement
t's almost impossible to ignore the impact that XML has had on today's applications, from the simplest client-side application storing user settings to an enterprise application with XML message passing. As a result, a number of frameworks and APIs have sprung up to simplify the task of reading and writing verbose XML files.

Each of these APIs focuses on solving a particular developer problem, such as providing an object-oriented view of XML through tools like XMLBeans and JAXB or an event view of XML through tools such as SAX and StAX. This article demonstrates how to combine two of these great APIs, XMLBeans (see Sidebar 1. What Is XMLBeans?) and StAX (see Sidebar 2. What Is StAX?), to obtain the benefits of each in your next application.

The obvious question is why use two APIs in the first place. The short answer is because you can. Each API offers its own pros and cons. For example, object binding (with XMLBeans, for example) provides easy-to-use XML validation and data access, but it tends to be memory heavy and inflexible. Streaming parsers (such as StAX) are lightweight, but they can be either a pain to deal with when extracting complex types or error prone when handling mixed content such as CDATA sections. The idea is to take the benefits of each API while limiting its downsides. As management might say, you're going to create synergy!



The Problem: Data Overload
Let's look at a common problem when dealing with XML. Suppose your company offers a service that allows businesses to stream payroll information to you in an XML format. You have already defined an XML schema for the data (see Listing 1 and Figure 1).

Click to enlarge

Figure 1. XML Schema for Payroll Data

The schema defines a company element, which contains a list of employees. Each employee contains some information, such as the amount he or she should be paid, the next pay date, and the address where the check should to be sent.

Ideally, you could write your application using XMLBeans to simply load the file and iterate over the employees, sending the information to the check printing, envelope stuffing, and mailing machine. This approach would abstract away all the details of XML parsing and validation and let you focus on the core business logic in a nice, clean, object-based design.

Unfortunately, some of your clients have tens of thousands of employees and you must be able to support multiple clients sending payroll information at the same time. Also, most object-binding APIs such as XMLBeans require the entire file to be present before they can start processing it, which is unacceptable because you don't want to wait for the last employee information to arrive before you begin to process the first.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap