Movatterモバイル変換


[0]ホーム

URL:


Document Information

Preface

Part I Introduction

1.  Overview

2.  Using the Tutorial Examples

Part II The Web Tier

3.  Getting Started with Web Applications

4.  Java Servlet Technology

5.  JavaServer Pages Technology

6.  JavaServer Pages Documents

7.  JavaServer Pages Standard Tag Library

8.  Custom Tags in JSP Pages

9.  Scripting in JSP Pages

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

Part III Web Services

16.  Building Web Services with JAX-WS

17.  Binding between XML Schema and Java Classes

18.  Streaming API for XML

Why StAX?

Streaming versus DOM

Pull Parsing versus Push Parsing

StAX Use Cases

Comparing StAX to Other JAXP APIs

StAX API

Cursor API

Iterator API

Iterator Event Types

Example of Event Mapping

Choosing between Cursor and Iterator APIs

Development Goals

Comparing Cursor and Iterator APIs

Using StAX

StAX Factory Classes

XMLInputFactory Class

XMLOutputFactory Class

XMLEventFactory Class

Resources, Namespaces, and Errors

Resource Resolution

Attributes and Namespaces

Error Reporting and Exception Handling

Reading XML Streams

UsingXMLStreamReader

UsingXMLEventReader

Writing XML Streams

UsingXMLStreamWriter

UsingXMLEventWriter

Sun's Streaming XML Parser Implementation

Reporting CDATA Events

Streaming XML Parser Factories Implementation

Example Code

Example Code Organization

Example XML Document

Cursor Example

Stepping through Events

Returning String Representations

Building and Running the Cursor Example Using NetBeans IDE

Building and Running the Cursor Example Using Ant

Cursor-to-Event Example

Instantiating anXMLEventAllocator

Creating an Event Iterator

Creating the Allocator Method

Building and Running the Cursor-to-Event Example Using NetBeans IDE

Building and Running the Cursor-to-Event Example Using Ant

Event Example

Creating an Input Factory

Creating an Event Reader

Creating an Event Iterator

Getting the Event Stream

Returning the Output

Building and Running the Event Example Using NetBeans IDE

Building and Running the Event Example Using Ant

Filter Example

Implementing theStreamFilter Class

Creating an Input Factory

Creating the Filter

Capturing the Event Stream

Filtering the Stream

Returning the Output

Building and Running the Filter Example Using NetBeans IDE

Building and Running the Filter Example Using Ant

Read-and-Write Example

Creating an Event Producer/Consumer

Creating an Iterator

Creating a Writer

Returning the Output

Building and Running the Read-and-Write Example Using NetBeans IDE

Building and Running the Read-and-Write Example Using Ant

Writer Example

Creating the Output Factory

Creating a Stream Writer

Writing the Stream

Returning the Output

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

Part IV Enterprise Beans

20.  Enterprise Beans

21.  Getting Started with Enterprise Beans

22.  Session Bean Examples

23.  A Message-Driven Bean Example

Part V Persistence

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

Part VI Services

28.  Introduction to Security in the Java EE Platform

29.  Securing Java EE Applications

30.  Securing Web Applications

31.  The Java Message Service API

32.  Java EE Examples Using the JMS API

33.  Transactions

34.  Resource Connections

35.  Connector Architecture

Part VII Case Studies

36.  The Coffee Break Application

37.  The Duke's Bank Application

Part VIII Appendixes

A.  Java Encoding Schemes

B.  About the Authors

Index

 

The Java EE 5 Tutorial

Java Coffee Cup logo
PreviousContentsNext

StAX API

The 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 API

As 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 API

The 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 Types

Table 18-2 lists theXMLEvent types defined in the event iterator API.

Table 18-2XMLEvent Types

Event Type

Description

StartDocument

Reportsthe beginning of a set of XML events, including encoding, XML version, andstandalone properties.

StartElement

Reports the start of an element, including any attributes and namespace declarations;also provides access to the prefix, namespace URI, and local name of thestart tag.

EndElement

Reports the end tag of an element. Namespaces that have goneout of scope can be recalled here if they have been explicitly seton their correspondingStartElement.

Characters

Corresponds to XMLCData sections andCharacterData entities. Notethat ignorable white space and significant white space are also reported asCharacterevents.

EntityReference

Character entities can be reported as discrete events, which an application developer canthen choose to resolve or pass through unresolved. By default, entities are resolved.Alternatively, if you do not want to report the entity as an event,replacement text can be substituted and reported asCharacters.

ProcessingInstruction

Reports the target anddata for an underlying processing instruction.

Comment

Returns the text of a comment.

EndDocument

Reports theend of a set of XML events.

DTD

Reports asjava.lang.String information about the DTD,if any, associated with the stream, and provides a method for returning customobjects found in the DTD.

Attribute

Attributes are generally reported as part of aStartElement event. However, there are times when it is desirable to return anattribute as a standaloneAttribute event; for example, when a namespace is returned asthe result of anXQuery orXPath expression.

Namespace

As with attributes, namespaces areusually reported as part of aStartElement, but there are times whenit is desirable to report a namespace as a discreteNamespace event.

Note that theDTD,EntityDeclaration,EntityReference,NotationDeclaration, andProcessingInstruction events are onlycreated if the document being processed contains a DTD.

Example of Event Mapping

As 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

#

Element/Attribute

Event

1

version="1.0"

StartDocument

2

isCData = falsedata = "\n"IsWhiteSpace = true

Characters

3

qname = BookCatalogue:http://www.publishing.orgattributes = nullnamespaces = {BookCatalogue" -> http://www.publishing.org"}

StartElement

4

qname = Bookattributes = nullnamespaces = null

StartElement

5

qname = Titleattributes = nullnamespaces = null

StartElement

6

isCData = falsedata = "Yogasana Vijnana: the Science of Yoga\n\t"IsWhiteSpace = false

Characters

7

qname = Titlenamespaces = null

EndElement

8

qname = ISBNattributes = nullnamespaces = null

StartElement

9

isCData = falsedata = "81-40-34319-4\n\t"IsWhiteSpace = false

Characters

10

qname = ISBNnamespaces = null

EndElement

11

qname = Costattributes = {"currency" -> INR}namespaces = null

StartElement

12

isCData = falsedata = "11.50\n\t"IsWhiteSpace = false

Characters

13

qname = Costnamespaces = null

EndElement

14

isCData = falsedata = "\n"IsWhiteSpace = true

Characters

15

qname = Booknamespaces = null

EndElement

16

isCData = falsedata = "\n"IsWhiteSpace = true

Characters

17

qname = BookCatalogue:http://www.publishing.orgnamespaces = {BookCatalogue" -> http://www.publishing.org"}

EndElement

18

EndDocument

There are several important things to note in this example:

  • The events are created in the order in which the corresponding XML elements are encountered in the document, including nesting of elements, opening and closing of elements, attribute order, document start and document end, and so forth.

  • As with proper XML syntax, all container elements have corresponding start and end events; for example, everyStartElement has a correspondingEndElement, even for empty elements.

  • Attribute events are treated as secondary events, and are accessed from their correspondingStartElement event.

  • Similar toAttribute events,Namespace events are treated as secondary, but appear twice and are accessible twice in the event stream, first from their correspondingStartElement and then from their correspondingEndElement.

  • Character events are specified for all elements, even if those elements have no character data. Similarly,Character events can be split across events.

  • The StAX parser maintains a namespace stack, which holds information about all XML namespaces defined for the current element and its ancestors. The namespace stack, which is exposed through thejavax.xml.namespace.NamespaceContext interface, can be accessed by namespace prefix or URI.

Choosing between Cursor and Iterator APIs

It 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 Goals

The authors of the StAX specification targeted three types of developers:

  • Library and infrastructure developers: Need highly efficient, low-level APIs with minimal extensibility requirements.

  • J2ME developers: Need small, simple, pull-parsing libraries, and have minimal extensibility needs.

  • Java EE and Java SE developers: Need clean, efficient pull-parsing libraries, plus need the flexibility to both read and write XML streams, create new event types, and extend XML document elements and attributes.

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 APIs

Before 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:

  • Objects created from theXMLEvent subclasses are immutable, and can be used in arrays, lists, and maps, and can be passed through your applications even after the parser has moved on to subsequent events.

  • You can create subtypes ofXMLEvent that are either completely new information items or extensions of existing items but with additional methods.

  • You can add and remove events from an XML event stream in much simpler ways than with the cursor API.

Similarly, keep some general recommendations in mind when making your choice:

  • If you are programming for a particularly memory-constrained environment, like J2ME, you can make smaller, more efficient code with the cursor API.

  • If performance is your highest priority (for example, when creating low-level libraries or infrastructure), the cursor API is more efficient.

  • If you want to create XML processing pipelines, use the iterator API.

  • If you want to modify the event stream, use the iterator API.

  • If you want your application to be able to handle pluggable processing of the event stream, use the iterator API.

  • In general, if you do not have a strong preference one way or the other, using the iterator API is recommended because it is more flexible and extensible, thereby “future-proofing” your applications.

PreviousContentsNext

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices


[8]ページ先頭

©2009-2025 Movatter.jp