CodesJava

Easy learning with example program codes

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.

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 codesjava
 */
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.

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.

 



Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki