Cover Page by Gary Cornell, Cay S. Horstmann

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

2.6.2. Using the StAX Parser

The StAX parser is a “pull parser.” Instead of installing an event handler, you simply iterate through the events, using this basic loop:

InputStream in = url.openStream(); XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader parser = factory.createXMLStreamReader(in); while (parser.hasNext()) {    int event = parser.next();    Call parser methods to obtain event details }

For example, when parsing the fragment

<font>    <name>Helvetica</name>    <size units="pt">36</size> </font>

the parser yields the following events:

1. START_ELEMENT, element name: font

2. CHARACTERS, content: white space

3. START_ELEMENT, element name: name

4. CHARACTERS, content: Helvetica

5. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required