Basic StAX Parsing Using Woodstox

Overview

Woodstox provides excellent support for StAX (Streaming API for XML) parsing. Streaming API is used to parse large XML documents in a performance efficient manner. StAX compliant parsers do not load the entire XML document in memory but use a ‘Pull Model’ to load sections of document and parse it.

Woodstox is compliant with JSR-173 standard.

In the following program, we shall explore how to perform basic StAX parsing using Woodstox.

Create the sample XML file as shown below

Create the TestBasicStaxParsing parser as shown below.

Load the XML file as an
InputStream
(see line 15 below).

Create
XMLStreamReader2
using
XMLInputFactory
class (see lines 16-17 below).

Iterate through
XMLStreamReader2
using hasNext() method (see line 18 below).

Get the event type using
XMLStreamReader2
‘s next() method (see line 19 below).

Based on the type of the event, extract the contents and print them to console (see lines 20-33 below).

The output of the program demonstrating basic StAX parsing is shown below:

Download Source Code for this Program

Leave a Comment