advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
StAX Source Code
Partners & Affiliates
advertisement
advertisement
advertisement
advertisement
Average Rating: 4.6/5 | Rate this item | 8 users have rated this item.
StAX: DOM Ease with SAX Efficiency (cont'd)

StAX Output

No discussion of StAX is complete without mentioning StAX output. StAX is bi-directional in that it supports both read and write. The StAX XMLStreamWriter class provides a simple, low-level API to output XML data.
advertisement

The following is an example of using StAX to generate an XML ATOM feed document:


File file = new File("atomoutput.xml");
FileOutputStream out = new FileOutputStream(file);
String now = new SimpleDateFormat().format(new Date(System.currentTimeMillis()));
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter staxWriter = factory.createXMLStreamWriter(out);
staxWriter.writeStartDocument("UTF-8", "1.0");
// feed
staxWriter.writeStartElement("feed");
staxWriter.writeNamespace("", "http://www.w3.org/2005/Atom");
// title
StaxUtil.writeElement(staxWriter,"title","Simple Atom Feed File");
// subtitle
StaxUtil.writeElement(staxWriter,"subtitle","Using StAX to read feed files");
// link
staxWriter.writeStartElement("link");
staxWriter.writeAttribute("href","http://example.org/");
staxWriter.writeEndElement();
// updated
StaxUtil.writeElement(staxWriter,"updated",now);
// author
...
// entry
..
staxWriter.writeEndElement(); // end feed
staxWriter.writeEndDocument();
staxWriter.flush();
staxWriter.close();

The resultant XML file is identical to the ATOM feed file previously shown in the "Patterns for Using StAX" section.

StAX Parsers

Several JSR-173-compliant parsers are available, including the following:
  1. Woodstox
  2. StAX Reference Implementation
  3. Oracle StAX Pull Parser
  4. BEA

You'll find the entire StAX JSR-173 on the JCP Web site.

Just How Fast Is StAX?

In addition to being easy to use, StAX is also very fast. Sun has released a whitepaper (PDF) that compares its performance with several other parsers.

The Future of StAX

StAX is ideally suited for no-nonsense, efficient XML input and output. The pull paradigm promotes a more intuitive parsing approach whereby application components can aggregate logically related parsing operations and pull what they want from the stream one element after another. Developers must still maintain appropriate state throughout the parsing process, but they retain overall control. StAX, like SAX, works well for large documents and when parts of the document can be dealt with in small chunks independently of other chunks. And best of all, it's fast.

Previous Page: Patterns for Using StAX  


Lara D'Abreo is an independent consultant with over 10 years experience in commercial product development in the US, Japan, and the UK. She's currently based out of Sydney, Australia and spends her time trying to make J2EE systems run faster.
Page 1: IntroductionPage 3: StAX Output
Page 2: Patterns for Using StAX 
Please rate this item (5=best)
 1  2  3  4  5
advertisement