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
01.
<
cricket
>
02.
<
team
rank
=
"1"
>
03.
<
name
>India</
name
>
04.
<
captain
>MS Dhoni</
captain
>
05.
</
team
>
06.
<
team
rank
=
"2"
>
07.
<
name
>South Africa</
name
>
08.
<
captain
>AB de Villiers</
captain
>
09.
</
team
>
10.
<
team
rank
=
"3"
>
11.
<
name
>Australia</
name
>
12.
<
captain
>Michael Clarke</
captain
>
13.
</
team
>
14.
<
team
rank
=
"4"
>
15.
<
name
>New zealand</
name
>
16.
<
captain
>Brendon McCullum</
captain
>
17.
</
team
>
18.
<
team
rank
=
"5"
>
19.
<
name
>SriLanka</
name
>
20.
<
captain
>Angelo Mathews</
captain
>
21.
</
team
></
cricket
>
We have Team class to represent Team element in xml document.
Team.java
01.
package
com.javatutorialcorner.stax;
02.
03.
public
class
Team {
04.
private
int
rank;
05.
private
String name;
06.
private
String captain;
07.
08.
public
int
getRank() {
09.
return
rank;
10.
}
11.
public
void
setRank(
int
rank) {
12.
this
.rank = rank;
13.
}
14.
public
String getName() {
15.
return
name;
16.
}
17.
public
void
setName(String name) {
18.
this
.name = name;
19.
}
20.
public
String getCaptain() {
21.
return
captain;
22.
}
23.
public
void
setCaptain(String captain) {
24.
this
.captain = captain;
25.
}
26.
}
Here is the class where we are using StAX XMLStreamReader to read the xml file to list of Team object.
StAXCursorReadExample.java
01.
package
com.javatutorialcorner.stax;
02.
03.
import
java.io.FileInputStream;
04.
import
java.io.FileNotFoundException;
05.
import
java.util.ArrayList;
06.
import
java.util.List;
07.
08.
import
javax.xml.stream.XMLInputFactory;
09.
import
javax.xml.stream.XMLStreamConstants;
10.
import
javax.xml.stream.XMLStreamException;
11.
import
javax.xml.stream.XMLStreamReader;
12.
13.
public
class
StAXCursorReadExample {
14.
private
static
boolean
name;
15.
private
static
boolean
captain;
16.
17.
public
static
void
main(String[] args) {
18.
String filePath =
"C:/jtc/cricket.xml"
;
19.
List<Team> topCricketTeams = parseXML(filePath);
20.
21.
for
(Team team : topCricketTeams) {
22.
System.out.println(
"************Team Start**************"
);
23.
System.out.println(
"Name : "
+team.getName());
24.
System.out.println(
"Rank : "
+team.getRank());
25.
System.out.println(
"Captain : "
+team.getCaptain());
26.
System.out.println(
"************Team End**************"
);
27.
}
28.
29.
}
30.
31.
private
static
List<team> parseXML(String filePath) {
32.
List<Team> topCricketTeams =
new
ArrayList<>();
33.
Team team =
null
;
34.
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
35.
try
{
36.
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(
new
FileInputStream(filePath));
37.
while
(xmlStreamReader.hasNext()) {
38.
int
eventType = xmlStreamReader.next();
39.
switch
(eventType) {
40.
case
XMLStreamConstants.START_ELEMENT:
41.
if
(xmlStreamReader.getLocalName().equals(
"Team"
)){
42.
team =
new
Team();
43.
team.setRank(Integer.parseInt(xmlStreamReader.getAttributeValue(
0
)));
44.
}
else
if
(xmlStreamReader.getLocalName().equals(
"Name"
)){
45.
name =
true
;
46.
}
else
if
(xmlStreamReader.getLocalName().equals(
"Captain"
)){
47.
captain =
true
;
48.
}
49.
break
;
50.
case
XMLStreamConstants.CHARACTERS:
51.
if
(name){
52.
team.setName(xmlStreamReader.getText());
53.
name =
false
;
54.
}
else
if
(captain){
55.
team.setCaptain(xmlStreamReader.getText());
56.
captain =
false
;
57.
}
58.
break
;
59.
case
XMLStreamConstants.END_ELEMENT:
60.
if
(xmlStreamReader.getLocalName().equals(
"Team"
)){
61.
topCricketTeams.add(team);
62.
}
63.
break
;
64.
}
65.
66.
}
67.
}
catch
(FileNotFoundException | XMLStreamException e) {
68.
e.printStackTrace();
69.
}
70.
return
topCricketTeams;
71.
}
72.
73.
}
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