In this tutorial we are going to see about how to write XML document using Java StAX parser Cursor API.
Player.java
Player.java
package com.javatutorialcorner.stax;
public class Player {
private int rank;
private String name;
private String team;
private int matches;
private int runs;
private int ratings;
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 getTeam() {
return team;
}
public void setTeam(String team) {
this.team = team;
}
public int getMatches() {
return matches;
}
public void setMatches(int matches) {
this.matches = matches;
}
public int getRuns() {
return runs;
}
public void setRuns(int runs) {
this.runs = runs;
}
public int getRatings() {
return ratings;
}
public void setRatings(int ratings) {
this.ratings = ratings;
}
}
StAXCursorWriteExample.java
package com.javatutorialcorner.stax;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
public class StAXCursorWriteExample {
public static void main(String[] args) {
String filePath = "C:/jtc/cricket1.xml";
Map<String,Player> elementsMap = new HashMap<String,Player>();
Player player1 = new Player();
player1.setName("AB de Villiers");
player1.setRank(1);
player1.setRatings(898);
player1.setMatches(184);
player1.setRuns(7777);
elementsMap.put("player1", player1);
Player player2 = new Player();
player2.setName("Hashim Amla");
player2.setRank(2);
player2.setRatings(849);
player2.setMatches(112);
player2.setRuns(5654);
elementsMap.put("player2", player2);
Player player3 = new Player();
player3.setName("Kumar Sangakkara");
player3.setRank(3);
player3.setRatings(849);
player3.setMatches(401);
player3.setRuns(13961);
elementsMap.put("player3", player3);
Player player4 = new Player();
player4.setName("Virat Kohli");
player4.setRank(4);
player4.setRatings(847);
player4.setMatches(154);
player4.setRuns(6451);
elementsMap.put("player4", player4);
writeXML(filePath,elementsMap);
}
private static void writeXML(String filePath, Map<String,Player> elementsMap) {
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
try {
XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(new FileOutputStream(filePath), "UTF-8");
xmlStreamWriter.writeStartDocument("UTF-8", "1.0");
xmlStreamWriter.writeCharacters("\n");
xmlStreamWriter.writeStartElement("Players");
for(Map.Entry<String,Player> entry : elementsMap.entrySet()){
Player player = entry.getValue();
xmlStreamWriter.writeCharacters("\n\t");
xmlStreamWriter.writeStartElement("Player");
xmlStreamWriter.writeAttribute("rank", String.valueOf(player.getRank()));
xmlStreamWriter.writeCharacters("\n\t\t");
xmlStreamWriter.writeStartElement("Name");
xmlStreamWriter.writeCharacters(player.getName());
xmlStreamWriter.writeCharacters("\n\t\t");
xmlStreamWriter.writeEndElement();
xmlStreamWriter.writeCharacters("\n\t\t");
xmlStreamWriter.writeStartElement("Team");
xmlStreamWriter.writeCharacters(player.getTeam());
xmlStreamWriter.writeCharacters("\n\t\t");
xmlStreamWriter.writeEndElement();
xmlStreamWriter.writeCharacters("\n\t\t");
xmlStreamWriter.writeStartElement("Rating");
xmlStreamWriter.writeCharacters(String.valueOf(player.getRatings()));
xmlStreamWriter.writeCharacters("\n\t\t");
xmlStreamWriter.writeEndElement();
xmlStreamWriter.writeCharacters("\n\t\t");
xmlStreamWriter.writeStartElement("Matches");
xmlStreamWriter.writeCharacters(String.valueOf(player.getMatches()));
xmlStreamWriter.writeCharacters("\n\t\t");
xmlStreamWriter.writeEndElement();
xmlStreamWriter.writeCharacters("\n\t\t");
xmlStreamWriter.writeStartElement("Runs");
xmlStreamWriter.writeCharacters(String.valueOf(player.getRuns()));
xmlStreamWriter.writeCharacters("\n\t\t");
xmlStreamWriter.writeEndElement();
xmlStreamWriter.writeCharacters("\n\t");
xmlStreamWriter.writeEndElement();
}
xmlStreamWriter.writeCharacters("\n");
xmlStreamWriter.writeEndDocument();
xmlStreamWriter.flush();
xmlStreamWriter.close();
} catch (FileNotFoundException | XMLStreamException e) {
e.printStackTrace();
}
}
}
Output
Here is output XML file
Cricket1.xml
AB de Villiers
898
184
7777
Hashim Amla
849
112
5654
Kumar Sangakkara
849
401
13961
Virat Kohli
847
154
6451
0 comments:
Post a Comment