Aalto: Basic StAX Parsing Using Aalto

Overview

Aalto provides JSR-173 compliant StAX (Streaming API for XML) parsing. The key classes involved in StAX parsing are
XMLInputFactory2
,
XMLStreamReader2
and
XMLEvent
. The following sample program demonstrates how to perform basic StAX parsing

Create employee.xml as shown below. It contains employee related information like id, name and salary (see lines 2-4 below)

Create TestBasicStaxParsing class as shown below.

Load Aalto’s parser factory using the name ‘com.fasterxml.aalto.stax.InputFactoryImpl’ as shown in line 17 below)

Instantiate
XMLStreamReader2
as shown in line 18 below.

Then iterate through the xml stream reader by using
XMLStreamReader2.next()
method (see line 19 below).

Get the event type (see line 20 below) and based on the type of element, extract parsed data (see lines 21-34 below)

The output of the program demonstrating basic StAX parsing is show below.

Download Source Code for this Program

Leave a Comment