We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 8 March 2015

Java StAX Parser – Read XML using StAX Iterator API

In this tutorial we are going to see how to read XML file using StAX Iterator API.For this tutorial we have following XML file that contains list of Cricket teams.
crcicket.xml

  
   India
   MS Dhoni
  
  
   South Africa
   AB de Villiers
  
  
   Australia
   Michael Clarke
  
  
   New zealand
   Brendon McCullum
  
  
   SriLanka
   Angelo Mathews
   


We have Team class to represent Team element in xml document.
Team.java

package com.javatutorialcorner.stax;

public class Team {
 private int rank;
 private String name;
 private String captain;
 
 public int getRank() {
  return rank;
 }
 public void setRank(int rank) {
  this.rank = rank;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getCaptain() {
  return captain;
 }
 public void setCaptain(String captain) {
  this.captain = captain;
 }
}


Here is the class where we are using StAX XMLEventReader to read the xml file to list of Team object.


StAXEventReadExample.java
package com.javatutorialcorner.stax;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class StAXEventReadExample {
 private static boolean name;
 private static boolean captain;
 public static void main(String[] args) {
  String filePath = "C:/jtc/cricket.xml";
  List<Team> topCricketTeams = parseXML(filePath);
  
  for (Team team : topCricketTeams) {
  System.out.println("************Team Start**************"); 
  System.out.println("Name : "+team.getName());
  System.out.println("Rank : "+team.getRank());
  System.out.println("Captain : "+team.getCaptain());
  System.out.println("************Team End**************");
  }

 }

 private static List<team> parseXML(String filePath) {
  List<Team> topCricketTeams = new ArrayList<>();
  Team team = null;
  XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
  try {
   XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader( new FileInputStream(filePath));
   while(xmlEventReader.hasNext()) {     
    XMLEvent xmlEvent  = xmlEventReader.nextEvent();
             switch(xmlEvent.getEventType()) {
                 case XMLEvent.START_ELEMENT:
                  StartElement startElement = xmlEvent.asStartElement();
                  String tagName = startElement.getName().getLocalPart();                  
                  if(tagName.equals("Team")){
                   team = new Team();
                   Iterator<attribute> attributes = startElement.getAttributes();
                         String rank = attributes.next().getValue();
                         team.setRank(Integer.parseInt(rank));
                  }else if(tagName.equals("Name")){
                   name = true;
                  }else if(tagName.equals("Captain")){
                   captain = true;
                  }                  
                     break;
                 case XMLEvent.CHARACTERS:
                  Characters characters = xmlEvent.asCharacters();
                        if(name){
                         team.setName(characters.getData());
                         name = false;
                        }else if(captain){
                         team.setCaptain(characters.getData());
                         captain = false;
                        }
                     break;   
                 case XMLEvent.END_ELEMENT:
                  EndElement endElement = xmlEvent.asEndElement();
                        if(endElement.getName().getLocalPart().equals("Team")){
                  topCricketTeams.add(team); 
                  }
                     break;
                 }
 
             }
  } catch (FileNotFoundException | XMLStreamException e) {
   e.printStackTrace();
  }
  return topCricketTeams;
 }
}

Output


************Team Start**************
Name : India
Rank : 1
Captain : MS Dhoni
************Team End**************
************Team Start**************
Name : South Africa
Rank : 2
Captain : AB de Villiers
************Team End**************
************Team Start**************
Name : Australia
Rank : 3
Captain : Michael Clarke
************Team End**************
************Team Start**************
Name : New zealand
Rank : 4
Captain : Brendon McCullum
************Team End**************
************Team Start**************
Name : SriLanka
Rank : 5
Captain : Angelo Mathews
************Team End**************

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Java StAX Parser – Read XML using StAX Iterator API Rating: 5 Reviewed By: eHowToNow