使用dom4j寫xml檔案——源碼

來源:互聯網
上載者:User

標籤:lower   src   ati   ima   on()   tty   uid   data   輸出   

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      * 產生uuid20      *21      * @return 32位uuid22      */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 args71      */72     public static void main(String[] args) throws Exception {73         writeXml();74     }75 }

 

輸出結果

 

使用dom4j寫xml檔案——源碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.