1. 程式人生 > >使用dom4j寫xml文件——源碼

使用dom4j寫xml文件——源碼

main workspace int rgs mat dom4j new space 指定

dom4j版本:2.1.1

使用dom4j生成xml文檔步驟:

1、創建一個Document對象的空白文檔。

2、向Document中添加根元素,返回的Element表示根元素,xml文檔只有一個根元素。

3、添加子元素。

4、創建XmlWriter對象

5、輸出Document對象

元素對象

 1 package cnblogs.testCSV;
 2 
 3 public class Person {
 4     private String id;
 5     private String name;
 6     private String sex;
 7     private
int age; 8 9 public Person() { 10 } 11 12 public Person(String id, String name, String sex, int age) { 13 this.id = id; 14 this.name = name; 15 this.sex = sex; 16 this.age = age; 17 } 18 19 public String getId() { 20 return id; 21 }
22 23 public void setId(String id) { 24 this.id = id; 25 } 26 27 public String getName() { 28 return name; 29 } 30 31 public void setName(String name) { 32 this.name = name; 33 } 34 35 public String getSex() { 36 return sex; 37 } 38 39
public void setSex(String sex) { 40 this.sex = sex; 41 } 42 43 public int getAge() { 44 return age; 45 } 46 47 public void setAge(int age) { 48 this.age = age; 49 } 50 }

寫xml

 1 package cnblogs.testXml;
 2 
 3 import cnblogs.testCSV.Person;
 4 import org.dom4j.Document;
 5 import org.dom4j.DocumentHelper;
 6 import org.dom4j.Element;
 7 import org.dom4j.io.OutputFormat;
 8 import org.dom4j.io.XMLWriter;
 9 
10 import java.io.FileOutputStream;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.UUID;
14 
15 public class FileXml {
16     private static final String fileName = "D:\\workspace\\tmp\\obj.xml";
17 
18     /**
19      * 生成uuid
20      *
21      * @return 32位uuid
22      */
23     private static String getUUID32() {
24         return UUID.randomUUID().toString().replace("-", "").toLowerCase();
25     }
26 
27     /**
28      * 構造數據
29      *
30      * @return 數據
31      */
32     private static List<Person> buildData() {
33         List<Person> personList = new ArrayList<Person>(10);
34         personList.add(new Person(getUUID32(), "張三", "female", 26));
35         personList.add(new Person(getUUID32(), "李四", "man", 34));
36         personList.add(new Person(getUUID32(), "王五", "female", 55));
37         personList.add(new Person(getUUID32(), "一一", "female", 11));
38         return personList;
39     }
40 
41     public static void writeXml() {
42         List<Person> personList = buildData();
43         Document doc = DocumentHelper.createDocument();
44         Element root = doc.addElement("personlist");
45         for (Person emp : personList) {
46             Element empEle = root.addElement("person");
47             Element nameEle = empEle.addElement("id");
48             nameEle.addText(emp.getId());
49             Element ageEle = empEle.addElement("name");
50             ageEle.addText(emp.getName());
51             Element genderEle = empEle.addElement("sex");
52             genderEle.addText(emp.getSex());
53             Element salEle = empEle.addElement("age");
54             salEle.addText(emp.getAge() + "");
55             empEle.addAttribute("id", emp.getId() + ""); //向當前元素中添加指定名字以及對應值的屬性
56         }
57         try {
58             XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
59             FileOutputStream fos = new FileOutputStream(fileName);
60             writer.setOutputStream(fos);
61             writer.write(doc);
62             writer.close();
63         }
64         catch (Exception e) {
65             e.printStackTrace();
66         }
67     }
68 
69     /**
70      * @param args
71      */
72     public static void main(String[] args) throws Exception {
73         writeXml();
74     }
75 }

輸出結果

技術分享圖片

使用dom4j寫xml文件——源碼