| Author |
Xml File generation - xstream
|
G.Sathish kumar
Ranch Hand
Joined: Jul 27, 2009
Posts: 84
|
|
i need to build the xml file from database resultset
- <chart caption="Country Comparison" shownames="1" showvalues="0" decimals="0" numberPrefix="$">
- <categories>
<category label="Austria" />
<category label="Brazil" />
<category label="France" />
<category label="Germany" />
<category label="USA" />
</categories>
- <dataset seriesName="1996" color="AFD8F8" showValues="0">
<set value="25601.34" />
<set value="20148.82" />
<set value="17372.76" />
<set value="35407.15" />
<set value="38105.68" />
</dataset>
- <dataset seriesName="1997" color="F6BD0F" showValues="0">
<set value="57401.85" />
<set value="41941.19" />
<set value="45263.37" />
<set value="117320.16" />
<set value="114845.27" />
</dataset>
- <dataset seriesName="1998" color="8BBA00" showValues="0">
<set value="45000.65" />
<set value="44835.76" />
<set value="18722.18" />
<set value="77557.31" />
<set value="92633.68" />
</dataset>
</chart>
i thought of using xstream is there any other good tools or api availble comparingly with xstream?
|
Thanks
Sathish kumar
SCJP, SCWCD
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12266
|
|
Why not just write the document with plain old Java println statements?
Bill
|
Java Resources at www.wbrogden.com
|
 |
G.Sathish kumar
Ranch Hand
Joined: Jul 27, 2009
Posts: 84
|
|
i solved this by using xstream but please tell me the below code will assist for more object expenses
i thought of using iteration on wherever required required
import java.util.List;
import java.util.ArrayList;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
public class ChartUtil
{
public static void main(String[] args)
{
XStream xstream = new XStream();
Chart oChart = new Chart();
oChart.setCaption("Model Comparison");
Category oCategory = new Category();
oCategory.setLabel("Lot Quantity");
oChart.addCategories(oCategory);
oCategory = new Category();
oChart.setCaption("Total Cost");
oChart.addCategories(oCategory);
Dataset oDataset = new Dataset();
oDataset.setSeriesName("Lot Quantity");
Set oSet = new Set("100");
oDataset.addSet(oSet);
oDataset.addSet(oSet);
oSet = new Set("101");
oDataset.addSet(oSet);
oSet = new Set("102");
oChart.addDataSet(oDataset);
oDataset = new Dataset();
oDataset.setSeriesName("Total Cost");
oSet = new Set("100");
oDataset.addSet(oSet);
oSet = new Set("101");
oDataset.addSet(oSet);
oSet = new Set("102");
oDataset.addSet(oSet);
oChart.addDataSet(oDataset);
xstream.addImplicitCollection(Chart.class, "dataSet");
xstream.autodetectAnnotations(true);
System.out.println(xstream.toXML(oChart));
}
}
@XStreamAlias("Chart")
class Chart
{
@XStreamAsAttribute
private String caption;
private List<Category> categories = new ArrayList<Category>();
private List<Dataset> dataSet = new ArrayList<Dataset>();
private List<Set> set = new ArrayList<Set>();
public Chart()
{
}
public void addCategories(Category ocategory) {
categories.add(ocategory);
}
public void addDataSet(Dataset oDataset) {
dataSet.add(oDataset);
}
public void addSet(Set oSet) {
set.add(oSet);
}
public void setCaption(String caption) {
this.caption = caption;
}
public String getCaption() {
return caption;
}
}
@XStreamAlias("category")
class Category
{
@XStreamAsAttribute
private String label;
Category()
{
}
public void setLabel(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
}
@XStreamAlias("dataset")
class Dataset
{
private List<Set> set = new ArrayList<Set>();
@XStreamAsAttribute
private String seriesName;
Dataset()
{
}
public void addSet(Set oSet)
{
set.add(oSet);
}
public String getSeriesName() {
return seriesName;
}
public void setSeriesName(String seriesName) {
this.seriesName = seriesName;
}
}
@XStreamAlias("set")
class Set
{
@XStreamAsAttribute
private String value;
Set(String valueLocal)
{
this.value = valueLocal;
}
public void setValue(String value)
{
this.value = value;
}
public String getValue()
{
return value;
}
}
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Please http://faq.javaranch.com/java/UseCodeTags.
Apart from XStream, you can look in this thread for tips on how to create an XML document.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
G.Sathish kumar
Ranch Hand
Joined: Jul 27, 2009
Posts: 84
|
|
can i know the puropose of
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
It's the XML document declaration. See http://en.wikipedia.org/wiki/Xml#Well-formedness for more info.
The standalone attribute is mostly absent, but you can read about it here.
|
 |
 |
|
|
subject: Xml File generation - xstream
|
|
|