Following jars are required to use XStream APIs.
- xpp3-1.1.3.4d_b4_min.jar
- xstream-1.1.2.jar
Initialization of XStream will work for Java-XML and XML-Java conversion if same set of tags and java objects are going to be used for conversion.
Lets see very simple example. In the first part following example is about converting java objects to following xml document. And in the second part the we will convert the same xml document into Java objects.
Part one – Converting Java objects into following XML document structure. The XML document will contain <team-player-mapping> as root tag and n number if <team teamid=”12”> tags. The <team> tag will have n number of player tags.
<team-player-mapping>
<team id=413>
<player>ASIF</player>
<player>RAM</player>
<player>GOLDU</player>
<player>VIKY</player>
<player>AJAY</player>
<player>MANOJ</player>
</team>
<team id=410>
<player>AKRAM</player>
<player>MOHAN</player>
<player>KISHOR</player>
</team>
<team id=408>
<player>RIK</player>
<player>ASTER</player>
<player>FOSTER</player>
<player>FSCP</player>
</team>
</team-player-mapping>
By Seeing the xml document structure, the Java classes will be in same hierarchy/structure, in short, the child tags will be member variables in java classes (referring parent tags in xml).
For Example:
public class TeamPlayerMapping implements Serializable{
List team; //collection on Team class objects,
//collection of <team> in <team-player-mapping>
public List getTeam() {
return team;
}
public void setTeam(List team) {
this.team = team;
}
}
public class Team implements Serializable {
private String id; //attribute teamid in <team>,
//will also be treated as child
private List player; //Collection of Player, <player> in <team>
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Team(){
}
public Team(String id){
setId(id);
}
public List getPlayer() {
return player;
}
public void setPlayer(List player) {
this.player = player;
}
}
public class Player{
String player;
public String getPlayer(){
return player;
}
public void setPlayer(String player){
this.player=player;
}
}
Initializing XStream object: In this part the proper hierarchy of classes is expressed to XStream object and aliasing the class name to proper xml tag or attribute name is expressed/set.
XStream xstream = new XStream();xstream.alias("team", Team.class); //"team" tag
xstream.alias(“team-player-mapping”, TeamPlayerMapping.class);
xstream.alias(“player”, String.class); //"player" tag
//Team List(collection) in TeamPlayerMapping - n number of Team tags in team-player-//mapping tag.
xstream.addImplicitCollection(TeamPlayerMapping.class, "team", Team.class);
//atribute of team tag - id is a String type member in Team class which will become attribute
//afer using following api.
xstream.useAttributeFor(Team.class, "id");
//player List(collection) in Team
xstream.addImplicitCollection(Team.class, “player”, String.class);
Setting the hierarchy of objects in XStream object. Here values are assigned in the objects in proper hierarchy and set into XStream to generate the xml format, shown above.
List teamL = new LinkedList();teamPlayerMapping.setTeam(teamL);
HashMap teamPlayer = loadPlayerMappings();
for(java.util.Iterator keys = teamPlayer.keySet().iterator(); keys.hasNext(); ){
String key = (String)(keys.next());//Team ID
//Add a new Team object in teamL collection.
Team team = new Team();
//Set collection of Player objects in Team object
List playerList = (List)teamPlayer.get(key);
team.setPlayer(playerList);
team.setId(key);
teamL.add(team);
}
return xstream.toXML(teamPlayerMapping).trim();
Following Returns a HashMap in a hierarchy like key as team id and value as list of players. This hashmap is converted into respective xml/xstream hierarchy in the code above.
public HashMap loadPlayerMappings() throws Exception{
HashMap teamPlayer = new LinkedHashMap();
try{
CachedRowSet cRset = this.getResultSet(SQL_SEL_ACCESS);
String tmpTeamID = "";
List playerList = new LinkedList();
while(cRset.next()){
String teamId = cRset.getString("TEAM_ID");
String playerId = cRset.getString("PLAYER_ID");
if(!tmpTeamID.equals(teamId)){
playerList = new LinkedList();
teamPlayer.put(teamId, playerList);
}
playerList.add(playerId);
tmpTeamID = cRset.getString("TEAM_ID");
}
}
catch(SQLException sqle){
throw new Exception(sqle, "Error While Selecting Team-Player Mapping");
}
return teamPlayer ;
}
Part two – Converting XML document/tags into java objects in hierarchy/ structure. Same initialization code will be used to identify the xml tags, and the java objects in which values/attributes of xml tags are supposed to be assigned.
String xml = "<team-player-mapping>"+ " <team id="413">"+ " <player>ASIF</player>"+ " <player>RAM</player>"+ " <player>GOLDU</player>"+ " <player>VIKY</player>"+ " <player>AJAY</player>"+ " <player>MANOJ</player>"+ " </team>"+ " <team id="410">"+ " <player>AKRAM</player>"+ " <player>MOHAN</player>"+ " <player>KISHOR</player>"+ " </team>"+ " <team id="408">"+ " <player>RIK</player>"+ " <player>ASTER</player>"+ " <player>FOSTER</player>"+ " <player>FSCP</player>"+ " </team>"+ "</team-player-mapping>";
To reconstruct an object, purely from the XML: TeamPlayerMapping teamPlayerMapping = (TeamPlayerMapping )xstream.fromXML(xml);
Conclusion is, only initialization part is most critical. Once it is done the Serialization and de-serialization can be done very easily. This is very elementary detail/tutorial about using XStream. More can be found on web.
No comments:
Post a Comment