Java StAX Cursor Based API Read XML Example

Java StAX API provides two XML processing API – cursor based and iterator based. Earlier we saw examples of iterator based API to read XML file and write XML file. Here we will learn how to use cursor based API to read XML file.

When we use StAX XML Parser, we need to create XMLInputFactory to read XML file. Then we can chose cursor based API by creating XMLStreamReader object to read file. XMLStreamReader next() method is used to get the next parsing event and it returns the int value depending on the event type. Common event types are Start Document, Start Element, Characters, End Element and End Document. XMLStreamConstants contains int constants that can be used to process events based on it’s type.

For this tutorial, we have below xml file that is list of Employee element.


We have Employee class to represent the Employee element.

Here is the class where we are using StAX XMLStreamReader to read the xml file to list of Employee object.

When we execute above program, we get following output in console.

By admin

Leave a Reply

%d bloggers like this: