Java StAX XMLStreamReader

StAX XMLStreamReader:

StAX XMLStreamReader class provides a Cursor style API which facilitates iteration over events. It is used for reading the XML documents.

Methods of StAX XMLStreamReader:

1. int next(): It is used to retrieve next event. 2. boolean hasNext(): It is used to check further events exists or not. 3. String getText(): It is used to get text of an element. 4. String getLocalName(): It is used to get name of an element.

Commonly used XML Stream Events:

1. ATTRIBUTE 2. CHARACTERS 3. COMMENT 4. END_DOCUMENT 5. END_ELEMENT 6. ENTITY_DECLARATION 7. ENTITY_REFERENCE 8. NAMESPACE 9. NOTATION_DECLARATION 10. PROCESSING_INSTRUCTION 11. SPACE 12. START_DOCUMENT 13. START_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 javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
 
/**
 * This class is used to read XML elements using Stax parser.
 * @author w3spoint
 */
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 XMLStreamReader object.
		XMLStreamReader xmlStreamReader  = 
                       xmlInputFactory.createXMLStreamReader(fileReader);
 
		//Iterate through events.
		while(xmlStreamReader.hasNext()){
		  //Get integer value of current event.
		  int xmlEvent = xmlStreamReader.next();
 
		  //Process start element.
		  if (xmlEvent == XMLStreamConstants.START_ELEMENT) {
			System.out.println("Start Element: "
                                        +xmlStreamReader.getLocalName());
			int attributes = 
                                     xmlStreamReader.getAttributeCount();
			for(int i=0; i<attributes; i++){
			 QName name = xmlStreamReader.getAttributeName(i);
			String value=xmlStreamReader.getAttributeValue(i);
			 System.out.println("Attribute name: " + name);
			 System.out.println("Attribute value: " + value);
			}
		  }
 
		  //Process end element.
		  if (xmlEvent == XMLStreamConstants.END_ELEMENT) {
			System.out.println("End Element: "
                                         +xmlStreamReader.getLocalName());
		  }
		}
	  } 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 XMLStreamWriter example. Previous Topic: Java StAX XMLEventWriter example.

 

Please follow and like us:
Content Protection by DMCA.com