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;
}
}