XStream – Basic Introduction
January 26, 2010 at 11:30 am Leave a comment
So you must be asking, where does the XStream topic come in considering most of my previous posts were around ICEfaces and Fusion Charts?
The Need
To answer this question let me try to explain what XStream does. XStream allows you to convert a Serializable Java Object into XML. See the link yet? The reason why I needed this was to dynamically create Fusion Charts (that require parameters in XML)
The XStream website: http://xstream.codehaus.org/index.html proves to be a very useful resource. As a side note I have been quite impressed lately by vendor websites, it seems much more time and effort being spent on actual documentation and that’s GREAT!
The Basics
Getting back to the topic at hand, so what does XStream allow us to do? Simply put generate XML from a Java Object. I am not too sure of how all it parses the objects but so far seems to work well.
Simple Example
As stated above the need for this came about as I required a dynamic piece of XML based on a Java object and since us Java Developers love objects XStream is a perfect partner.
I found working backwards help, define the way you want your XML to look. Since we want to create a basic Pie chart our XML needs to look like this:
<chart caption="Not Started"> <set label="Not Started" value="25"/> <set label="Started" value="50"/> <set label="In Between" value="25"/> </chart>
To do this I created two classes:
Chart.java
**
* This class forms the basis of our chart
*/
import java.util.ArrayList;
import java.util.List;
public class Chart {
private String caption;
private String subCaption;
private List labels = new ArrayList();
public Chart (String caption) {
setCaption(caption);
}
public void add(Entry entry) {
labels.add(entry);
}
public List getContent() {
return labels;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public void setSubCaption(String subCaption) {
this.subCaption = subCaption;
}
public String getSubCaption() {
return subCaption;
}
}
Entry.java
public class Entry {
private String label, value;
public Entry(String label, String value) {
this.label = label;
this.value = value;
}
}
These two classes provided us with the ability to create a chart and add entries to it (entries would be values). What we then required was a Main class to process this data. You need to add the xstream-[version].jar to your build path. Download the jar from the following location: http://xstream.codehaus.org/download.html , select the XStream Core only
Sample Main.Java
import com.thoughtworks.xstream.XStream;
public class Main {
/**
* <!--fc:renderHTML>
*/
public static void main(String[] args) {
XStream xStreamOne = new XStream ();
xStreamOne.alias("chart", Chart.class);
xStreamOne.alias("set", Entry.class);
xStreamOne.addImplicitCollection(Chart.class, "labels");
xStreamOne.useAttributeFor(Chart.class, "caption");
xStreamOne.useAttributeFor(Chart.class, "subCaption");
xStreamOne.useAttributeFor(Entry.class, "label");
xStreamOne.useAttributeFor(Entry.class, "value");
Chart chart = new Chart("Not Started");
chart.setSubCaption("Sub caption is");
chart.add(new Entry("Not Started", "25"));
chart.add(new Entry("Started", "50"));
chart.add(new Entry("In Between", "25"));
xml = xStreamOne.toXML(chart);
System.out.println(xml);
}
}
This results in the required output:
<chart caption="Not Started"> <set label="Not Started" value="25"/> <set label="Started" value="50"/> <set label="In Between" value="25"/> </chart>
The main methods to note are these three:
xStreamOne.alias("set", Entry.class);
xStreamOne.addImplicitCollection(Chart.class, "labels");
xStreamOne.useAttributeFor(Chart.class, "caption");
For further details have a look at this example: http://xstream.codehaus.org/alias-tutorial.html
Blog Soon!
Entry filed under: ICEfaces. Tags: .
Trackback this post | Subscribe to the comments via RSS Feed