• June 26, 2015
  • XML Parser
  • Comments Off on Java StAX XMLEventReader example

Java StAX XMLEventReader example

StAX XMLEventReader:

StAX XMLEventReader class provides an iterator style API which facilitates iteration over events. It is used for reading the XML documents.

Methods of StAX XMLEventReader:

1. StartElement asStartElement() : It is used to retrieve value and attributes of element.
2. EndElement asEndElement(): It called at the end of an element.
3. Characters asCharacters(): It can be used to obtain characters such a CDATA, whitespace etc.
4. Boolean isStartElement(): It is used to determine the start element.
5. Boolean isEndElement(): It is used to determine the end element.



<?xml version="1.0" ?>
  <student name="Rajesh Garg"></student>
  <student name="Harish Kansal"></student>


import java.io.FileReader;
import java.io.Reader;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
 * This class is used to read XML elements using Stax parser.
 * @author javawithease
public class StaxTest {
	public static void main(String args[]){
	  try {
		//File Path
		String filePath = "D:\\classNew.xml";
		//Read XML file.
		Reader fileReader = new FileReader(filePath);
		//Get XMLInputFactory instance.
		XMLInputFactory xmlInputFactory=
		//Create XMLEventReader object.
		XMLEventReader xmlEventReader = 
		//Iterate through events.
		  //Get next event.
		  XMLEvent xmlEvent = xmlEventReader.nextEvent();
		  //Check if event is the start element.
		  if (xmlEvent.isStartElement()) {
		     //Get event as start element.
		     StartElement startElement=xmlEvent.asStartElement();
		     System.out.println("Start Element: "
					+ startElement.getName());
		     //Iterate and process attributes.
		     Iterator iterator = startElement.getAttributes();
		     while (iterator.hasNext()) {
			  Attribute attribute=(Attribute) iterator.next();
			  QName name = attribute.getName();
			  String value = attribute.getValue();
			  System.out.println("Attribute name: " + name);
			  System.out.println("Attribute value: " + value);
		  //Check if event is the end element.
		  if (xmlEvent.isEndElement()) {
			//Get event as end element.
			EndElement endElement = xmlEvent.asEndElement();
			System.out.println("End Element: "
					+ endElement.getName());
	  } catch (Exception e) {


Start Element: class
Start Element: student
Attribute name: name
Attribute value: Rajesh Garg
End Element: student
Start Element: student
Attribute name: name
Attribute value: Harish Kansal
End Element: student
End Element: class

Download this example.
Next Topic: Java StAX XMLEventWriter example.
Previous Topic: Java Stax XML parser.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Comments are closed.