Tutorials : Parsing with StAX in JDK 6.0 :

Parsing with XMLEventReader

This section shows you how to parse catalog.xml using XMLEventReader. The XMLEventReader interface parses an XML document using an event object iterator; this way, an XML event generates an XMLEvent object. XMLEventReader is similar to XMLStreamReader in that parsing events are generated by the StAX parser. However, XMLEventReader has an advantage over XMLStreamReader: using XMLEventReader, an application may peek at the next event using the peek() method, without reading the event from the stream. Thus, an application client may determine the suitability of parsing the next event. The code snippets in this section are taken from the XMLEventParser.java application, listed in Listing 5.

First, import the StAX classes:

import javax.xml.stream.*;
import javax.xml.stream.events.*;
import javax.xml.stream.XMLInputFactory;
Next, create an XMLInputFactory from which to obtain an XMLEventReader object:
XMLInputFactory inputFactory=XMLInputFactory.newInstance();
InputStream input=new FileInputStream(new File("C:/STAX/catalog.xml"));
XMLEventReader  xmlEventReader =inputFactory.createXMLEventReader(input);
In StAX, XML document events are represented by XMLEvent objects. Use nextEvent() method to iterate over the XMLEventReader object to obtain the next event:
XMLEvent event=xmlEventReader.nextEvent(); 
Obtain the event type using getEventType() method (refer to Table 1 for a list of the different event typesin StAX. The XMLEvent interface also provides boolean methods to obtain the event types. For example, the isStartDocument() returns true if the event is of type start document. In the following code, the event is of type start element, so a StartElement object is be obtained from the XMLEvent interface:
if(event.isStartElement()){
StartElement startElement=event.asStartElement();
}
Obtain the element attributes using getAttributes() method:
Iterator attributes=startElement.getAttributes();
The Iterator represents a javax.xml.stream.events.Attribute objects. Iterate over the Iterator using next() method.
Attribute attribute=(javax.xml.stream.events.Attribute)(attributes.next());
Lastly, obtain the attribute name using getName() method and the attribute value using getValue() method.

Listing 5 shows the Java application to parsing the XML document. Application XMLEventReader may be run as a command line application, or in an IDE such as Eclipse. Remember: if you run the XMLWriter.java or XMLParser.java applications without first running the XMLEventParser.java application, you'll need to copy catalog.xml to the C:/StAX directory.

Ultimately, pull-based event generation provides event regulation to the parser application rather than the parser.

Home / Articles / Parsing with StAX in JDK 6.0 / 1 / 2 / 3 / 4 /

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.