Java StAX XMLEventReader

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.

Example:

classNew.xml

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

StaxTest.java

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 w3schools
 */
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=
                                  XMLInputFactory.newInstance();
 
		//Create XMLEventReader object.
		XMLEventReader xmlEventReader = 
                       xmlInputFactory.createXMLEventReader(fileReader);
 
		//Iterate through events.
		while(xmlEventReader.hasNext()){
		  //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) {
		e.printStackTrace();
	  }
	}
}

Output:

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.