StAX Parser Example

On Aug 1, 2015 by digvijayb |

Using StAX Parser

Parsing an XML Document with StAX parser which is one of the fastest way to parser document in current days. Having very less memory footprint, easy to implement then previous generation of parser like

DOM (Document Object Model) parse and others. This make StAX must use API for modern day application it is fast and lightweight. So, below we have a easy program to parse down an XML document using StAX.

Overview StAX Pull Model

StAX (Streaming API for XML) is an application programming interface (API) to read and write XML documents, originating from the Java programming language community. It is one most fastest API to parse an XML document and very lightweight too. StAX work’s on pull style of parsing unlike to SAX parser which is push style parsing.

Feature of StAX

  • Use Pull Model approach to parser a document.
  • Memory footprint is very small
  • Available in JDK 1.6 onward. No need of any addition jar
  • Most efficiently access XML (especially with cursor API).
  • Very easy to use.

In the following example an XML document contain list of user information. And we are going to parser that XML document list of user object java. “Let’s Stack Up The Code”


<?xml version="1.0" encoding="UTF-8"?>
    <user id="1">
        <name>Digvijay Bhakuni</name>
        <location>Gurgaon, India</location>
    <user id="2">
        <name>Rahul Sharma</name>
        <location>New Delhi, India</location>
    <user id="3">
        <name>Pankaj Bhakser</name>
        <location>Gurgaon, India</location>
    <user id="4">
        <name>Pankaj Mishar</name>
        <location>Nodia, India</location>
        <user id="5">
        <name>Ajay Garg</name>
        <location>Gurgaon, India</location>

Now creating POJO this object will hole the value of user.


 * @author digvijayb
public class UserVO {

	public UserVO() {

	private int id;
	private String name;
	private String userid;
	private String password;
	private String gender;
	private int age;
	private String location;

	public int getId() {
		return id;
	public void setId(int id) { = id;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public String getUserid() {
		return userid;
	public void setUserid(String userid) {
		this.userid = userid;
	public String getPassword() {
		return password;
	public void setPassword(String password) {
		this.password = password;
	public String getGender() {
		return gender;
	public void setGender(String gender) {
		this.gender = gender;
	public int getAge() {
		return age;
	public void setAge(int age) {
		this.age = age;
	public String getLocation() {
		return location;
	public void setLocation(String location) {
		this.location = location;
	public String toString(){
		StringBuilder builder = new StringBuilder("{ ");
		builder.append("id : ").append(id).append(", ");
		builder.append("name : ").append(name).append(", ");
		builder.append("userid : ").append(userid).append(", ");
		builder.append("password : ").append(password).append(", ");
		builder.append("gender : ").append(gender).append(", ");
		builder.append("age : ").append(age).append(", ");
		builder.append("location : ").append(location).append(" }");
		return builder.toString();

Using StAX API to read Xml


import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;


 * @author digvijayb
public class StAXReader {

	public List<UserVO> getUserList(String path) throws FileNotFoundException, XMLStreamException{
		StartElement startElement = null;
		EndElement endElement = null;
		Attribute attribute = null;
		QName idAttr =new QName("id"); 
		XMLEvent event = null;
		List<UserVO> list = null;
		UserVO userVO = null;
		String currentValue = "";
		XMLInputFactory inputFactory = XMLInputFactory.newInstance();
		XMLEventReader reader  = inputFactory.createXMLEventReader(new  FileReader(path));

			event = reader.nextEvent();
				startElement = event.asStartElement();
					list = new ArrayList<UserVO>();
				}else if("user".equals(startElement.getName().getLocalPart())){ 
					userVO = new UserVO();
					attribute = startElement.getAttributeByName(idAttr);
			}else if(event.isEndElement()){
				endElement = event.asEndElement();
				}else if("user".equals(endElement.getName().getLocalPart())){ 
					userVO = null;
				}else if("name".equals(endElement.getName().getLocalPart())){
				}else if("username".equals(endElement.getName().getLocalPart())){
				}else if("password".equals(endElement.getName().getLocalPart())){
				}else if("gender".equals(endElement.getName().getLocalPart())){
				}else if("age".equals(endElement.getName().getLocalPart())){
				}else if("location".equals(endElement.getName().getLocalPart())){
			}else if (event.isCharacters() && userVO != null && startElement != null) {
				System.out.println(event.asCharacters().getData() +" "+startElement.getName().getLocalPart() );
				currentValue = event.asCharacters().getData();
		return list;


Call StAXReader Class from main method


import java.util.List;



 * @author digvijayb
public class StAXReaderExce {

	 * @param args
	public static void main(String[] args) {
		try {
			String path = "/home/digvijayb/App/test/user.xml";
			StAXReader stAXReader = new StAXReader();
			List<UserVO> list = stAXReader.getUserList(path);
		} catch (FileNotFoundException | XMLStreamException e) {



  { id : 1, name : Digvijay Bhakuni, userid : digvijayb, password : password1, gender : Male, age : 22, location : Gurgaon, India }, 
  { id : 2, name : Rahul Sharma, userid : rahuls, password : password2, gender : Male, age : 22, location : New Delhi, India },
  { id : 3, name : Pankaj Bhakser, userid : pbhakser, password : password3, gender : Male, age : 29, location : Gurgaon, India },
  { id : 4, name : Pankaj Mishar, userid : pankajm, password : password4, gender : Male, age : 23, location : Nodia, India },
  { id : 5, name : Ajay Garg, userid : ajayg, password : password5, gender : Male, age : 25, location : Gurgaon, India }

Please do comment and follow us for more interesting programming trick and tutorial.

Share :

Subscribe & Social

Subscribe to this blog via RSS.

Recent Posts


Java 14

Python 2

Wordpress 1

Ops 3

Shared 3

Angular 1

Web 1

Draftblog 1

Popular Tags

Java (14) Python (2) Wordpress (1) Ops (3) Angular (1) Web (1) Draftblog (1)


DGStack is web blog by digvijay bhakuni. This blog is about computer programming tips and trick. And some short tutorial and gettings started post about the framework, libriay, api and much more. The blog target mostly open source stuff like java, j2ee, nodeJs, angularJs, spring, struts, mongodb, mean, etc


This website claims no credit for any images posted on this site unless otherwise noted. Images on this blog are copyright to its respectful owners. If there is an image appearing on this blog that belongs to you and do not wish for it appear on this site, please E-mail with a link to said image and it will be promptly removed. And the information is best of my knowledge one can always write comment or mail regarding information is this blog.