Introduction to streaming API for XML (Stax)

Source: Internet
Author: User

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();}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.