dukai314 2010-12-13 11:07
浏览 248
已采纳

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条回答 默认 最新

  • iteye_13492 2010-12-14 00:28
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞
  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包