In this tutorial we are going to see about StAX cursor API to read an XML document. In the Cursor example, the application instructs the parser to read the next event in the XML input stream by calling next().
Note that next() just returns an integer constant corresponding to an underlying event where the parser is positioned. The application needs to call the relevant function to get more information related to the underlying event.
You can imagine this approach as a virtual cursor moving across the XML input stream. There are various accessor methods which can be called when that virtual cursor is at a particular event.
Sample XML
crcicket.xml
Note that next() just returns an integer constant corresponding to an underlying event where the parser is positioned. The application needs to call the relevant function to get more information related to the underlying event.
You can imagine this approach as a virtual cursor moving across the XML input stream. There are various accessor methods which can be called when that virtual cursor is at a particular event.
Sample XML
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 XMLStreamReader to read the xml file to list of Team object.
StAXCursorReadExample.java
package com.javatutorialcorner.stax;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
public class StAXCursorReadExample {
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 {
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new FileInputStream(filePath));
while(xmlStreamReader.hasNext()) {
int eventType = xmlStreamReader.next();
switch(eventType) {
case XMLStreamConstants.START_ELEMENT:
if(xmlStreamReader.getLocalName().equals("Team")){
team = new Team();
team.setRank(Integer.parseInt(xmlStreamReader.getAttributeValue(0)));
}else if(xmlStreamReader.getLocalName().equals("Name")){
name = true;
}else if(xmlStreamReader.getLocalName().equals("Captain")){
captain = true;
}
break;
case XMLStreamConstants.CHARACTERS:
if(name){
team.setName(xmlStreamReader.getText());
name = false;
}else if(captain){
team.setCaptain(xmlStreamReader.getText());
captain = false;
}
break;
case XMLStreamConstants.END_ELEMENT:
if(xmlStreamReader.getLocalName().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**************
0 comments:
Post a Comment