3Dflash 图表xml数据封装

package com.byd.core;

import java.io.IOException;   
import org.dom4j.Document;   
import org.dom4j.DocumentHelper;   
import org.dom4j.Element;   
/**  
 * 使用dom4j生成XML工具类  
 *   
 * @author Sarin  
 *   
 */  
public class XMLUtils {   
    private Document document = null;   
  
    public Document getDocument() {   
        return document;   
    }   
    /**  
     * 构造方法,初始化Document  
     */  
    public XMLUtils() {  
        document = DocumentHelper.createDocument();   
    }   
    /**  
     * 生成根节点  
     *   
     * @param rootName  
     * @return  
     */  
    public Element addRoot(String rootName) {   
        Element root = document.addElement(rootName);   
        return root;   
    }   
    /**  
     * 生成节点  
     *   
     * @param parentElement  
     * @param elementName  
     * @return  
     */  
    public Element addNode(Element parentElement, String elementName) {   
        Element node = parentElement.addElement(elementName);   
        return node;   
    }   
    /**  
     * 为节点增加一个属性  
     *   
     * @param thisElement  
     * @param attributeName  
     * @param attributeValue  
     */  
    public void addAttribute(Element thisElement, String attributeName,   
            String attributeValue) {   
        thisElement.addAttribute(attributeName, attributeValue);   
    }   
    /**  
     * 为节点增加多个属性  
     *   
     * @param thisElement  
     * @param attributeNames  
     * @param attributeValues  
     */  
    public void addAttributes(Element thisElement, String[] attributeNames, String[] attributeValues) {   
        for (int i = 0; i < attributeNames.length; i++) {   
            thisElement.addAttribute(attributeNames[i], attributeValues[i]);   
        }   
    }   
    /**  
     * 增加节点的值  
     *   
     * @param thisElement  
     * @param text  
     */  
    public void addText(Element thisElement, String text) {   
        thisElement.addText(text);   
    }   
    /**  
     * 获取最终的XML  
     *   
     * @return  
     * @throws IOException  
     */  
    public String getXML() {   
        return document.asXML().substring(39);   
    }   
}  


package com.byd.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Element;

import com.byd.core.BaseAction;
import com.byd.core.XMLUtils;
import com.byd.entity.Model;

@SuppressWarnings("serial")
public class ChartAction extends BaseAction{
	private String xmlStr;
	/**
	 * @return the xmlStr
	 */
	public String getXmlStr() {
		return xmlStr;
	}
	/**
	 * @param xmlStr the xmlStr to set
	 */
	public void setXmlStr(String xmlStr) {
		this.xmlStr = xmlStr;
	}
	public String dayChart() throws Exception{
		 XMLUtils xml = new XMLUtils();
		 Element chart = xml.addRoot("chart");  
		 xml.addAttribute(chart, "caption", "单日计划下达率");  
		 xml.addAttribute(chart, "basefontsize", "12");  
		 xml.addAttribute(chart, "xAxisName", "浏览器类型");
		 xml.addAttribute(chart, "yAxisName", "数量");
		 xml.addAttribute(chart, "formatNumberScale", "0");
		 xml.addAttribute(chart, "decimals", "0");// 小数精确度,0为精确到个位  
		 xml.addAttribute(chart, "showValues", "0");// 在报表上不显示数值  
		 List list = new ArrayList();
		 Model model = new Model();
		 Model model1 = new Model();
		 Model model2 = new Model();
		
		 list.add(model);
		 list.add(model1);
		 list.add(model2);
		 for (int i = 0; i < list.size(); i++) {  
		       Map item = (HashMap) list.get(i);  
		       Element set = xml.addNode(chart, "set");  
		       set.addAttribute("label", item.get("statVar").toString());  
		       set.addAttribute("value", item.get("statCount").toString());  
		       set.addAttribute("color", Integer.toHexString(  
		       (int) (Math.random() * 255 * 255 * 255)).toUpperCase());  
		 }  
		 xmlStr = xml.getXML(); 
		 System.out.println(xmlStr);
		 return SUCCESS;  
	}
}

 

转载于:https://www.cnblogs.com/qq1988627/archive/2011/06/16/6606947.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值