• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Xml File generation - xstream

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not just write the document with plain old Java println statements?

Bill
 
G.Sathish kumar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
G.Sathish kumar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can i know the puropose of

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
That feels good. Thanks. Here's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic