用dom4j生成XML文件

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.URISyntaxException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class rzXML1 {

public void createXML(String value,String name) {
//用工厂类创建一个document实例
Document doc = DocumentHelper.createDocument();
//创建根元素emps
Element rootEle = doc.addElement("Chart");
//添加注释
// rootEle.addComment("这是一个dom4j生成的xml文件");
rootEle.addAttribute("bgColor","#C3C3C3");
rootEle.addAttribute("upperLimit","100");
rootEle.addAttribute("lowerLimit","0");
rootEle.addAttribute("baseFontColor","FFFFFF");
rootEle.addAttribute("majorTMNumber","11");
rootEle.addAttribute("majorTMColor","FFFFFF");
rootEle.addAttribute("majorTMHeight","8");
rootEle.addAttribute("minorTMNumber","5");
rootEle.addAttribute("minorTMColor","FFFFFF");
rootEle.addAttribute("minorTMHeight","3");
rootEle.addAttribute("toolTipBorderColor","FFFFFF");
rootEle.addAttribute("toolTipBgColor","333333");
rootEle.addAttribute("gaugeOuterRadius", "98");
rootEle.addAttribute("gaugeOriginX", "100");
rootEle.addAttribute("gaugeOriginY", "95");
rootEle.addAttribute("gaugeScaleAngle", "270");
rootEle.addAttribute("placeValuesInside", "1");
rootEle.addAttribute("gaugeInnerRadius", "80%25");
rootEle.addAttribute("annRenderDelay", "0");
rootEle.addAttribute("gaugeFillMix", "");
rootEle.addAttribute("pivotRadius", "15");
rootEle.addAttribute("showPivotBorder", "0");
rootEle.addAttribute("pivotFillMix", "{CCCCCC},{333333}");
rootEle.addAttribute("pivotFillRatio", "100,100");
rootEle.addAttribute("showShadow", "1");
//emps根节点下创建一个emp节点
Element empEle = rootEle.addElement("colorRange");
//emp添加属性id="1"
//empEle.addAttribute("id", "1");
//emp节点下创建一个name节点
Element nameEle = empEle.addElement("color");
//name节点下创建一个文本节点zhangsan
nameEle.addAttribute("minValue", "0");
nameEle.addAttribute("maxValue", "50");
nameEle.addAttribute("code", "C1E1C1");
nameEle.addAttribute("alpha", "40");
//再为name节点创建一个兄弟节点
Element sexEle = empEle.addElement("color");
sexEle.addAttribute("minValue", "50");
sexEle.addAttribute("maxValue", "85");
sexEle.addAttribute("code", "F6F164");
sexEle.addAttribute("alpha", "40");

Element Ele = empEle.addElement("color");
Ele.addAttribute("minValue", "85");
Ele.addAttribute("maxValue", "120");
Ele.addAttribute("code", "F70118");
Ele.addAttribute("alpha", "40");

Element aa = rootEle.addElement("dials");
Element disEle = aa.addElement("dial");
disEle.addAttribute("value", value);
disEle.addAttribute("borderColor","FFFFFF");
disEle.addAttribute("bgColor", "000000,CCCCCC,000000");
disEle.addAttribute("borderAlpha", "10");
disEle.addAttribute("baseWidth", "8");


Element tionds = rootEle.addElement("annotations");
Element groupEle = tionds.addElement("annotationGroup");
groupEle.addAttribute("xPos", "90");
groupEle.addAttribute("yPos", "86");
groupEle.addAttribute("showBelow", "1");

Element action = groupEle.addElement("annotation");
action.addAttribute("type", "circle");
action.addAttribute("xPos", "10");
action.addAttribute("yPos", "10");
action.addAttribute("radius", "100");
action.addAttribute("startAngle", "0");
action.addAttribute("endAngle", "360");
action.addAttribute("fillColor", "CCCCCC,111111");
action.addAttribute("fillPattern", "linear");
action.addAttribute("fillAlpha", "100,100");
action.addAttribute("fillRatio", "100,100");
action.addAttribute("fillAngle", "-45");

Element action1 = groupEle.addElement("annotation");
action1.addAttribute("type", "circle");
action1.addAttribute("xPos", "10");
action1.addAttribute("yPos", "10");
action1.addAttribute("radius", "100");
action1.addAttribute("startAngle", "0");
action1.addAttribute("endAngle", "360");
action1.addAttribute("fillColor", "CCCCCC,111111");
action1.addAttribute("fillPattern", "linear");
action1.addAttribute("fillAlpha", "100,100");
action1.addAttribute("fillRatio", "69,69");
action1.addAttribute("fillAngle", "-45");

Element action2 = groupEle.addElement("annotation");
action2.addAttribute("type", "circle");
action2.addAttribute("xPos", "10");
action2.addAttribute("yPos", "10");
action2.addAttribute("radius", "100");
action2.addAttribute("startAngle", "0");
action2.addAttribute("endAngle", "360");
action2.addAttribute("color", "666666");

Element action3 = groupEle.addElement("annotation");
action3.addAttribute("type", "text");
action3.addAttribute("label", name);
action3.addAttribute("xPos", "10");
action3.addAttribute("yPos", "70");
action3.addAttribute("fontColor", "FFFFFF");
action3.addAttribute("fontSize", "12");
action3.addAttribute("isBold", "1");


String filePath="";//绝对路径
String webPath="xml/rz1.xml";//动态文件名 相对



try {
filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
filePath =filePath.replace("WEB-INF/classes/", "");
filePath += webPath;
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//将document中的内容写入文件中
try {
Writer out = new FileWriter(filePath);
//格式化输出,类型IE浏览一样
OutputFormat format = OutputFormat.createPrettyPrint();
//OutputFormat format = OutputFormat.createCompactFormat();
format.setEncoding("gbk");
//创建写出对象
XMLWriter writer = new XMLWriter(out,format);
writer.write(doc);
writer.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("失败了。");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值