2. Using the Tutorial Examples 3. Getting Started with Web Applications 5. JavaServer Pages Technology 7. JavaServer Pages Standard Tag Library 10. JavaServer Faces Technology 11. Using JavaServer Faces Technology in JSP Pages 12. Developing with JavaServer Faces Technology 13. Creating Custom UI Components 14. Configuring JavaServer Faces Applications 15. Internationalizing and Localizing Web Applications 16. Building Web Services with JAX-WS 17. Binding between XML Schema and Java Classes Pull Parsing versus Push Parsing Comparing StAX to Other JAXP APIs Resources, Namespaces, and Errors Error Reporting and Exception Handling Sun's Streaming XML Parser Implementation Streaming XML Parser Factories Implementation Returning String Representations Building and Running the Cursor Example Using NetBeans IDE Building and Running the Cursor Example Using Ant Instantiating anXMLEventAllocator Building and Running the Cursor-to-Event Example Using NetBeans IDE Building and Running the Cursor-to-Event Example Using Ant Building and Running the Event Example Using NetBeans IDE Building and Running the Event Example Using Ant Implementing theStreamFilter Class Building and Running the Filter Example Using NetBeans IDE Building and Running the Filter Example Using Ant Creating an Event Producer/Consumer Building and Running the Read-and-Write Example Using NetBeans IDE Building and Running the Read-and-Write Example Using Ant Building and Running the Writer Example Using NetBeans IDE Building and Running the Writer Example Using Ant Further Information about StAX 19. SOAP with Attachments API for Java 21. Getting Started with Enterprise Beans 23. A Message-Driven Bean Example 24. Introduction to the Java Persistence API 25. Persistence in the Web Tier 26. Persistence in the EJB Tier 27. The Java Persistence Query Language 28. Introduction to Security in the Java EE Platform 29. Securing Java EE Applications 31. The Java Message Service API 32. Java EE Examples Using the JMS API 36. The Coffee Break Application | StAX APIThe StAX API exposes methods for iterative, event-based processing of XML documents. XMLdocuments are treated as a filtered series of events, and infoset states canbe stored in a procedural fashion. Moreover, unlike SAX, the StAX API isbidirectional, enabling both reading and writing of XML documents. The StAX API is really two distinct API sets: acursor API andaniterator API. These two API sets explained in greater detail later inthis chapter, but their main features are briefly described below. Cursor APIAs the name implies, the StAXcursor API represents a cursor with whichyou can walk an XML document from beginning to end. This cursor canpoint to one thing at a time, and always moves forward, neverbackward, usually one infoset element at a time. The two main cursor interfaces areXMLStreamReader andXMLStreamWriter.XMLStreamReader includes accessor methodsfor all possible information retrievable from the XML Information model, including document encoding,element names, attributes, namespaces, text nodes, start tags, comments, processing instructions, document boundaries, andso forth; for example: public interface XMLStreamReader { public int next() throws XMLStreamException; public boolean hasNext() throws XMLStreamException; public String getText(); public String getLocalName(); public String getNamespaceURI(); // ... other methods not shown}You can call methods onXMLStreamReader, such asgetText andgetName, toget data at the current cursor location.XMLStreamWriter provides methods that correspond toStartElementandEndElement event types; for example: public interface XMLStreamWriter { public void writeStartElement(String localName) throws XMLStreamException; public void writeEndElement() throws XMLStreamException; public void writeCharacters(String text) throws XMLStreamException;// ... other methods not shown}The cursor API mirrors SAX in many ways. For example, methods areavailable for directly accessing string and character information, and integer indexes can be usedto access attribute and namespace information. As with SAX, the cursor API methodsreturn XML information as strings, which minimizes object allocation requirements. Iterator APIThe StAXiterator API represents an XML document stream as a set ofdiscrete event objects. These events are pulled by the application and provided bythe parser in the order in which they are read in the sourceXML document. The base iterator interface is calledXMLEvent, and there are subinterfaces for eachevent type listed inTable 18-2. The primary parser interface for reading iteratorevents isXMLEventReader, and the primary interface for writing iterator events isXMLEventWriter. TheXMLEventReader interface contains five methods, the most important of which isnextEvent, which returns the next event in an XML stream.XMLEventReader implementsjava.util.Iterator,which means that returns fromXMLEventReader can be cached or passed intoroutines that can work with the standard Java Iterator; for example: public interface XMLEventReader extends Iterator { public XMLEvent nextEvent() throws XMLStreamException; public boolean hasNext(); public XMLEvent peek() throws XMLStreamException; ...}Similarly, on the output side of the iterator API, you have: public interface XMLEventWriter { public void flush() throws XMLStreamException; public void close() throws XMLStreamException; public void add(XMLEvent e) throws XMLStreamException; public void add(Attribute attribute) throws XMLStreamException; ...}Iterator Event TypesTable 18-2 lists theXMLEvent types defined in the event iterator API. Table 18-2XMLEvent Types
Note that theDTD,EntityDeclaration,EntityReference,NotationDeclaration, andProcessingInstruction events are onlycreated if the document being processed contains a DTD. Example of Event MappingAs an example of how the event iterator API maps an XMLstream, consider the following XML document: <?xml version="1.0"?><BookCatalogue xmlns="http://www.publishing.org"> <Book> <Title>Yogasana Vijnana: the Science of Yoga</Title> <ISBN>81-40-34319-4</ISBN> <Cost currency="INR">11.50</Cost> </Book></BookCatalogue> This document would be parsed into eighteen primary and secondary events, as showninTable 18-3. Note that secondary events, shown in curly braces ({}), are typically accessedfrom a primary event rather than directly. Table 18-3 Example of Iterator API Event Mapping
There are several important things to note in this example:
Choosing between Cursor and Iterator APIsIt is reasonable to ask at this point, “What API should Ichoose? Should I create instances ofXMLStreamReader orXMLEventReader? Why are there twokinds of APIs anyway?” Development GoalsThe authors of the StAX specification targeted three types of developers:
Given these wide-ranging development categories, the StAX authors felt it was more usefulto define two small, efficient APIs rather than overloading one larger and necessarilymore complex API. Comparing Cursor and Iterator APIsBefore choosing between the cursor and iterator APIs, you should note a fewthings that you can do with the iterator API that you cannotdo with cursor API:
Similarly, keep some general recommendations in mind when making your choice:
Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |