package test;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import com.tiandy.easy7.core.util.CLS_Inquest_Type;
import com.tiandy.easy7.core.vo.CLS_VO_InformationDetermine;
/**
*
* <p> 项目名称:Easy7 </p>
* <p> 类 名 称:CreatXml </p>
* <p> 类 描 述: 生成xml类 </p>
* <p> 创 建 人:mxb </p>
* <p> @version:1.0 2013-5-4 下午09:51:10 mxb </p>
*/
public class CreatXml {
public static void main(String[] args) {
List<student> stuList = new ArrayList<student>();
stuList.add(new student("mxb", 20L));
stuList.add(new student("ly", 23L));
stuList.add(new student("zpf", 22L));
stuList.add(new student("chf", 20L));
stuList.add(new student("zfx", 18L));
stuList.add(new student("qgz", 28L));
create_Xml(stuList);
}
public static void create_Xml(List<student> stuList){
Document document = DocumentHelper.createDocument(); // 创建文档实例
Element catalogElement = document.addElement("chart"); // 创建根元素
catalogElement.addAttribute("caption", "archive").addAttribute(
"xAxisName", "Archive Type").addAttribute("yAxisName", "Archive Num").addAttribute(
"showValues", "0").addAttribute("formatNumberScale", "0").addAttribute("showBorder", "1");
for (student stu : stuList) {
Element journalElement = catalogElement.addElement("set"); // 添加子元素
journalElement.addAttribute("label", stu.getName()).addAttribute(
"value", stu.getAge().toString()); // 添加属性
}
OutputFormat format = OutputFormat.createPrettyPrint(); // 格式化输出
format.setEncoding("utf-8");
try {
String path = CLS_Inquest_Type.DATAXML_FILE_PATH;
XMLWriter output = new XMLWriter(new FileWriter(path+"Data11111111.xml"), format);
output.write(document);
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*
* <p> 项目名称:Easy7 </p>
* <p> 类 名 称:student </p>
* <p> 类 描 述: 学生实体类 </p>
* <p> 创 建 人:mxb </p>
* <p> @version:1.0 2013-5-4 下午09:49:59 mxb </p>
*/
class student{
private String name;
private Long age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getAge() {
return age;
}
public void setAge(Long age) {
this.age = age;
}
public student(String name, Long age) {
super();
this.name = name;
this.age = age;
}
}
最后生成的文件为:
<?xml version="1.0" encoding="utf-8"?>
<chart caption="archive" xAxisName="Archive Type" yAxisName="Archive Num" showValues="0" formatNumberScale="0" showBorder="1">
<set label="mxb" value="20"/>
<set label="ly" value="23"/>
<set label="zpf" value="22"/>
<set label="chf" value="20"/>
<set label="zfx" value="18"/>
<set label="qgz" value="28"/>
</chart>