Reference: http://www.ibm.com/developerworks/cn/xml/x-stax1.html? S_tact = 105agx52 & s_cmp = techcsdn
Streaming API for XML (Stax) is the latest standard for processing XML in Java. As a stream-oriented method, both performance and availability are superior to other methods, such as Dom and sax.
Stax Overview
From the very beginning, Java API for XML Processing (JAXP) provides two methods to process XML: the Document Object Model (DOM) method uses a standard Object Model to represent XML documents; the Simple API for XML (SAX) method uses the event handler provided by the application to process XML. JSR-173 proposes a new method for stream-oriented: streaming API for XML (Stax ). Its final version was released in March 2004 and will become part of JAXP 1.4 (which will be included in the forthcoming Java 6 release.
Complete example of xmlstreamreader parsing XML documents
XMLInputFactory factory = XMLInputFactory.newInstance();XMLStreamReader r = factory.createXMLStreamReader(input);try { int event = r.getEventType(); while (true) { switch (event) { case XMLStreamConstants.START_DOCUMENT: out.println("Start Document."); break; case XMLStreamConstants.START_ELEMENT: out.println("Start Element: " + r.getName()); for(int i = 0, n = r.getAttributeCount(); i < n; ++i) out.println("Attribute: " + r.getAttributeName(i) + "=" + r.getAttributeValue(i)); break; case XMLStreamConstants.CHARACTERS: if (r.isWhiteSpace()) break; out.println("Text: " + r.getText()); break; case XMLStreamConstants.END_ELEMENT: out.println("End Element:" + r.getName()); break; case XMLStreamConstants.END_DOCUMENT: out.println("End Document."); break; } if (!r.hasNext()) break; event = r.next(); }} finally { r.close();} |