Aalto provides a unique feature to parse XML documents asynchronously i.e. the
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.
for the given XML file (see line 19 below).
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
, in practice, multiple
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
(see line 24 below).
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
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
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
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”
I think sample code may have a problem in that content (XML document) is fed to parser twice: first when constructing parser, and then again incrementally. Doing so will result in error.
Problem was reported as:
Thanks for that comment, Will fix that bug soon.