Basics Of Asynchronous StAX Parsing Using Aalto

Overview

Aalto provides a unique feature to parse XML documents asynchronously i.e. the
InputStream
is not blocked when performing StAX based parsing.

For further details, please see
Introduction to Asynchronous StAX Parsing Using Aalto

This article demonstrates the programming aspects for using the asynchronous parsing feature of Aalto.

Create employee.xml as shown below. This XML shall be parsed by Aalto in asynchronous mode.

Create TestAsyncParsing.java as shown below.

Get the
InputStream
for the given XML file (see line 19 below).

Convert the
InputStream
as string (see line 21 below) and further to byte array (see line 22 below). This byte array shall be used to extract chunks of data to be fed to the asynchronous parser.

Note that even though, in this example, the byte array is extracted from only one
InputStream
, in practice, multiple
InputStream
s can contribute to the data to be pushed to the parser. This illustrates the non-blocking nature of the asynchronous parser.

An important first step towards asynchronous parsing is to create
AsyncXMLInputFactory
(see line 24 below).

Then create
AsyncXMLStreamReader
which shall read and parse the XML in an asynchronous manner (see line 26 below).

In asynchronous mode, a key breakaway from existing StAX parsing idiom is the concept of
AsyncInputFeeder
which provides a way to feed data to the parser (see line 27 below). This is keeping in mind the ‘Push Model’ for asynchronous parsing.

For this example, let us keep the buffer length of data to be fed to the async parser as ‘1’ (see line 28 below).

Keep looping until the end of document is reached (see lines 31 and 63 below).

Also create an inner loop which would run as long as the the event in incomplete (see line 33 below).Read 1 byte at a time from the input buffer (see line 34-35 below) and feed it to the parser using
AsyncInputFeeder.feedInput()
method (see line 36 below). Keep feeding the parser till the end of XML reached (see lines 38-40 below).

Extract the output of parsing process using standard StAX API (see lines 43-61 below).

Finally close the stream using
AsyncXMLStreamReader.close()
method (see line 65 below).

The output of the program demonstrating asynchronous parsing is shown below.

In particular note that the async parser parses 1 byte at a time (e.g. see lines 9-12 below)

Download Source Code for this Program

2 thoughts on “Basics Of Asynchronous StAX Parsing Using Aalto

Leave a Reply to Cowtowncoder Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Bitnami