如果是数据库里出来的,我猜应该有持有化bean结构吧
比如定义这种bean结构
[code="java"]package org.chart.xml.bean;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
-
最顶层GraphTag
*/
@XmlRootElement(name="graph")
public class GraphBean {
private String caption;
private String xAxisName;
private String yAxisName;
private int decimalPrecision;
private int formatNumberScale;
private List detailList = new ArrayList();
private TrendLinesBean trendLines;
@XmlAttribute
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
@XmlAttribute
public String getxAxisName() {
return xAxisName;
}
public void setxAxisName(String xAxisName) {
this.xAxisName = xAxisName;
}
@XmlAttribute
public String getyAxisName() {
return yAxisName;
}
public void setyAxisName(String yAxisName) {
this.yAxisName = yAxisName;
}
@XmlAttribute
public int getDecimalPrecision() {
return decimalPrecision;
}
public void setDecimalPrecision(int decimalPrecision) {
this.decimalPrecision = decimalPrecision;
}
@XmlAttribute
public int getFormatNumberScale() {
return formatNumberScale;
}
public void setFormatNumberScale(int formatNumberScale) {
this.formatNumberScale = formatNumberScale;
}
@XmlElement(name="set")
public List getDetailList() {
return detailList;
}
public void setDetailList(List detailList) {
this.detailList = detailList;
}
@XmlElement(name="trendlines")
public TrendLinesBean getTrendLines() {
return trendLines;
}
public void setTrendLines(TrendLinesBean trendLines) {
this.trendLines = trendLines;
}
/**
- 增加一个Set标签
*/
public void addDetail(DetailInfoBean bean) {
this.detailList.add(bean);
}
}[/code]
[code="java"]package org.chart.xml.bean;
import javax.xml.bind.annotation.XmlAttribute;
/**
-
Set标签内容
*/
public class DetailInfoBean {
private String name;
private String value;
private String color;
private String hoverText;
@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlAttribute
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@XmlAttribute
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@XmlAttribute
public String getHoverText() {
return hoverText;
}
public void setHoverText(String hoverText) {
this.hoverText = hoverText;
}
}[/code]
[code="java"]package org.chart.xml.bean;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
/**
-
TreadLines标签内容
*/
public class TrendLinesBean {
private List lineList = new ArrayList();
@XmlElement(name="line")
public List getLineList() {
return lineList;
}
public void setLineList(List lineList) {
this.lineList = lineList;
}
public void addLine(LineBean bean) {
this.lineList.add(bean);
}
}[/code]
[code="java"]package org.chart.xml.bean;
import javax.xml.bind.annotation.XmlAttribute;
/**
-
Line标签内容
*/
public class LineBean {
private String startValue;
private String color;
private String displayValue;
private String showOnTop;
@XmlAttribute
public String getStartValue() {
return startValue;
}
public void setStartValue(String startValue) {
this.startValue = startValue;
}
@XmlAttribute
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@XmlAttribute
public String getDisplayValue() {
return displayValue;
}
public void setDisplayValue(String displayValue) {
this.displayValue = displayValue;
}
@XmlAttribute
public String getShowOnTop() {
return showOnTop;
}
public void setShowOnTop(String showOnTop) {
this.showOnTop = showOnTop;
}
}[/code]
以上4个bean均为pojo。一般支持ORM的框架都会返回这种形式的数据结构。
接下来测试类:
[code="java"]package org.chart.xml.bean;
import java.io.PrintWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class Test {
/**
* 测试
*/
private Test() throws JAXBException {
// 模拟ORM从DB中出数据
GraphBean graphBean = initGraphTag();
addDetail(graphBean, "Jan", "462", "AFD8F8", "Janssss.xxx");
addDetail(graphBean, "Feb", "857", "F6BD0F");
addDetail(graphBean, "Mar", "671", "8BBA00");
addDetail(graphBean, "Apr", "494", "FF8E46");
addDetail(graphBean, "May", "761", "008E8E");
addDetail(graphBean, "Jun", "960", "D64646");
addDetail(graphBean, "Jul", "629", "8E468E");
addDetail(graphBean, "Aug", "622", "588526");
addDetail(graphBean, "Sep", "376", "B3AA00");
addDetail(graphBean, "Oct", "494", "008ED6");
addDetail(graphBean, "Nov", "761", "9D080D");
addDetail(graphBean, "Dec", "960", "A186BE");
addTrendlines(graphBean);
// 生成XML
printXml(graphBean);
}
private GraphBean initGraphTag() {
GraphBean graphBean = new GraphBean();
graphBean.setCaption("Monthly Unit Sales");
graphBean.setxAxisName("Month");
graphBean.setyAxisName("Units");
graphBean.setDecimalPrecision(0);
graphBean.setFormatNumberScale(0);
return graphBean;
}
private void addDetail(GraphBean graphBean, String... args) {
DetailInfoBean detailInfo = new DetailInfoBean();
if (args.length > 0) {
detailInfo.setName(args[0]);
}
if (args.length > 1) {
detailInfo.setValue(args[1]);
}
if (args.length > 2) {
detailInfo.setColor(args[2]);
}
if (args.length > 3) {
detailInfo.setHoverText(args[3]);
}
graphBean.addDetail(detailInfo);
}
private void addTrendlines(GraphBean graphBean) {
TrendLinesBean trendLines = new TrendLinesBean();
addLine(trendLines, "700", "91c728", "AVG", "1");
graphBean.setTrendLines(trendLines);
}
private void addLine(TrendLinesBean trendLines, String... args) {
LineBean line = new LineBean();
if (args.length > 0) {
line.setStartValue(args[0]);
}
if (args.length > 1) {
line.setColor(args[1]);
}
if (args.length > 2) {
line.setDisplayValue(args[2]);
}
if (args.length > 3) {
line.setShowOnTop(args[3]);
}
trendLines.addLine(line);
}
private void printXml(GraphBean graphBean) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(GraphBean.class);
Marshaller marshaller = context.createMarshaller();
// 生成文件的话请将这里的System.out替换成FileOutputStream或者直接文件path
PrintWriter writer = new PrintWriter(System.out);
marshaller.marshal(graphBean, writer);
writer.close();
}
/**
* @param args
* @throws JAXBException
*/
public static void main(String[] args) throws JAXBException {
new Test();
}
}
[/code]
在控制台看结果就可以看到你的xml了
这招叫jaxb2。是jdk6新增的方法,也是最快的方法,同时也是离OO和pojo最接近的方法。只需在javabean中配置annotation即可实现。一次配置永远可用