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

Java StAX Parser – StAX Iterator API Write Example

In this tutorial we are going to see about how to write XML document using Java StAX parser Iterator API.
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;
}

}
StAXEventWriteExample.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.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class StAXEventWriteExample {

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 {
XMLEventWriter xmlEventWriter = xmlOutputFactory
.createXMLEventWriter(new FileOutputStream(filePath), "UTF-8");
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
XMLEvent end = eventFactory.createDTD("\n");
StartDocument startDocument = eventFactory.createStartDocument();
xmlEventWriter.add(startDocument);
xmlEventWriter.add(end);
StartElement configStartElement = eventFactory.createStartElement("",
"", "Cricket");
xmlEventWriter.add(configStartElement);
xmlEventWriter.add(end);
StartElement players = eventFactory.createStartElement("",
"", "Players");
xmlEventWriter.add(players);
xmlEventWriter.add(end);
for(Map.Entry<string player=""> entry : elementsMap.entrySet()){
Player player = entry.getValue();
XMLEvent newLine = eventFactory.createDTD("\n");
XMLEvent tab = eventFactory.createDTD("\t");
StartElement startElement = eventFactory.createStartElement("", "", "Player");
xmlEventWriter.add(tab);
xmlEventWriter.add(startElement);
xmlEventWriter.add(newLine);

StartElement startName = eventFactory.createStartElement("", "", "Name");
xmlEventWriter.add(tab);
xmlEventWriter.add(tab);
xmlEventWriter.add(startName);
Characters characters1 = eventFactory.createCharacters(player.getName());
xmlEventWriter.add(characters1);
EndElement endName = eventFactory.createEndElement("", "", "Name");
xmlEventWriter.add(tab);
xmlEventWriter.add(tab);
xmlEventWriter.add(endName);
xmlEventWriter.add(newLine);

StartElement startRatings = eventFactory.createStartElement("", "", "Ratings");
xmlEventWriter.add(tab);
xmlEventWriter.add(tab);
xmlEventWriter.add(startRatings);
Characters characters3 = eventFactory.createCharacters(String.valueOf(player.getRatings()));
xmlEventWriter.add(characters3);
EndElement endRatings = eventFactory.createEndElement("", "", "Ratings");
xmlEventWriter.add(tab);
xmlEventWriter.add(tab);
xmlEventWriter.add(endRatings);
xmlEventWriter.add(newLine);

StartElement startMatches = eventFactory.createStartElement("", "", "Matches");
xmlEventWriter.add(tab);
xmlEventWriter.add(tab);
xmlEventWriter.add(startMatches);
Characters characters4 = eventFactory.createCharacters(String.valueOf(player.getMatches()));
xmlEventWriter.add(characters4);
EndElement endMatches = eventFactory.createEndElement("", "", "Matches");
xmlEventWriter.add(tab);
xmlEventWriter.add(tab);
xmlEventWriter.add(endMatches);
xmlEventWriter.add(newLine);

StartElement startRun = eventFactory.createStartElement("", "", "Runs");
xmlEventWriter.add(tab);
xmlEventWriter.add(tab);
xmlEventWriter.add(startRun);
Characters characters5 = eventFactory.createCharacters(String.valueOf(player.getRuns()));
xmlEventWriter.add(characters5);
EndElement endRun = eventFactory.createEndElement("", "", "Runs");
xmlEventWriter.add(tab);
xmlEventWriter.add(tab);
xmlEventWriter.add(endRun);
xmlEventWriter.add(newLine);

EndElement endElement = eventFactory.createEndElement("", "", "Player");
xmlEventWriter.add(tab);
xmlEventWriter.add(endElement);
xmlEventWriter.add(newLine);

}
xmlEventWriter.add(eventFactory.createEndElement("", "", "Players"));
xmlEventWriter.add(end);
xmlEventWriter.add(eventFactory.createEndElement("", "", "Cricket"));
xmlEventWriter.add(end);
xmlEventWriter.add(eventFactory.createEndDocument());
xmlEventWriter.close();

} catch (FileNotFoundException | XMLStreamException e) {
e.printStackTrace();
}

}

}


Output
cricket1.xml




AB de Villiers
898
184
7777


Hashim Amla
849
112
5654


Kumar Sangakkara
849
401
13961


Virat Kohli
847
154
6451

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 – StAX Iterator API Write Example Rating: 5 Reviewed By: eHowToNow