Using Aalto Wrapper Class For Asynchronous StAX Parsing


The basics of asynchronous stax parsing using Aalto is covered in the article
mentioned here
. In that article, you will note that low level buffer manipulation is required to manage asynchronous stax parsing.

However, Aalto’s unit test suite provides a very convenient wrapper class that internally handles the details of byte level management making the client code very convenient.

In this article, we shall how to use Aalto’s Wrapper class for asynchronous StAX parsing of an XML document.

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

Create AsyncReaderWrapper class as shown below. This class is borrowed from Aalto’s Unit test suite. This class is primarily responsible for handling performing byte level management of data in asynchronous mode thereby making the client code very clean and easy to maintain.

Create contructors that accept
, ‘bytesPerCall’ and the XML string to be parsed (see lines 20-29 below). The ‘bytesPerCall’ parameter defines the size of the buffer chunk that shall be pushed to the asynchronous parser.

Note the details of ‘nextToken()’ method that gets the next token from the
by performing manipulation of the buffer (see lines 41-59).

Create TestAsyncUsingWrapper as shown below.

It uses the
class mentioned above.

In particular note that 1 byte is read at a time (see line 24 below)

Note that this code is totally devoid of any details regarding byte level management. This makes this code much easier to maintain when compared the java code mentioned in
this article

Using this approach leads to better separtion of concerns and hence it this approach is recommended for asynchronous parsing.

The output of the program demonstrating the usage of Wrapper class for asynchronous parsing is shown below.

Download Source Code for this Program

Leave a Comment