JAVA如何生成不是很规则的XML文件

从java对象到xml不是难事,用xstream就可以,但是这是生成的规则的xml文件,如果有这样一个文件呢?

 

<graph caption="Monthly Unit Sales" xAxisName="Month" yAxisName="Units" decimalPrecision="0" formatNumberScale="0">
     <set name="Jan" value="462" color="AFD8F8" hoverText="Janssss.xxx" /> 
     <set name="Feb" value="857" color="F6BD0F" /> 
     <set name="Mar" value="671" color="8BBA00" /> 
     <set name="Apr" value="494" color="FF8E46" /> 
     <set name="May" value="761" color="008E8E" /> 
     <set name="Jun" value="960" color="D64646" /> 
     <set name="Jul" value="629" color="8E468E" /> 
     <set name="Aug" value="622" color="588526" /> 
     <set name="Sep" value="376" color="B3AA00" /> 
     <set name="Oct" value="494" color="008ED6" /> 
     <set name="Nov" value="761" color="9D080D" /> 
     <set name="Dec" value="960" color="A186BE" /> 
     <trendlines>
         <line startValue="700" color="91c728" displayValue="AVG" showOnTop="1" /> 
     </trendlines>
</graph>

  

 

   这个是FusionCharts,一个图型报表js,现在的要求就是从数据库读数据,生成XML然后供此JS读取生成图表

 

  现在好像只有一个办法,就是读-->string-->append--write(XML),目前我只发现这一种方法,不知道大家还好什么好的方法没有?

 

 或者是有没有更好的报表JS,供我参考下,但这次的重点还是怎么用java生成不是很规则的XML

 

3个回答

如果是数据库里出来的,我猜应该有持有化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即可实现。一次配置永远可用

使用dom4j啊
只要是标准xml就可以生成

不是很规则的XML文件 ?
上面的 xml 内容也是一个规则的 xml,与 xml 相关的标准有很多,形式有多样。
[code="java"] /**
* 生成 xml 内容
*/
public String generate() {

    ByteArrayOutputStream bos = null;
    try {
        bos = new ByteArrayOutputStream(1024);
        // 新建 format 用来格式化 xml 文件
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");                         // 编码格式
        format.setIndentSize(4);                             // Tab 缩进
        XMLWriter writer = new XMLWriter(bos, format);
        Document document = DocumentHelper.createDocument(); // 创建文档
        Element graph = document.addElement("graph");        // 创建根节点
        graph.addAttribute("caption", "Monthly Unit Sales");
        // add set 
        Element node = graph.addElement("set");
        node.addAttribute("name", "Jan");
        node.addAttribute("value", "462");
        // add trendlines 
        node = graph.addElement("trendlines");
        // add line 
        node = node.addElement("line");
        node.addAttribute("startValue", "700");
        node.addAttribute("color", "91c728");

        writer.write(document);
        writer.flush();
        writer.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException ioe) {
    }
    return bos.toString();
}[/code]

输出内容:
[code="html"]<?xml version="1.0" encoding="utf-8"?>






[/code]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问