Java&Xml教程(八)使用JDOM將Java對象轉換為XML

來源:互聯網
上載者:User

Java&Xml教程(八)使用JDOM將Java對象轉換為XML

在前面的教程中我們學習了如何使用JDOM解析和修改XML檔案內容,本節介紹如何將Java對象轉換為XML資料並組建檔案。
JDOM的Document類提供了便捷的方法建立元素和屬性,XMLOutputter 類能將Document寫到任何OutputStream和Writer對象中。
在本例中我們建立Employee對象集合并將它寫到XML檔案中。
Employee.java

package com.journaldev.xml;public class Employee {    private int id;    private String name;    private String gender;    private int age;    private String role;    public Employee(){}    public Employee(int id, String name, int age, String gender, String role){        this.id=id;        this.age=age;        this.name=name;        this.gender=gender;        this.role=role;    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getGender() {        return gender;    }    public void setGender(String gender) {        this.gender = gender;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public String getRole() {        return role;    }    public void setRole(String role) {        this.role = role;    }}

我們將Employee對象id作為XML檔案中Employee元素的屬性,並且設定根項目Employees的命名空間為“http://www.journaldev.com/employees”
JDOMXMLWriter.java

package com.journaldev.xml.jdom;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.jdom2.Document;import org.jdom2.Element;import org.jdom2.Namespace;import org.jdom2.output.Format;import org.jdom2.output.XMLOutputter;import com.journaldev.xml.Employee;public class JDOMXMLWriter {    public static void main(String[] args) throws IOException {        List empList = new ArrayList<>();        empList.add(new Employee(1, "Pankaj",25,"Male","Java Developer"));        empList.add(new Employee(2, "Mona",34,"Female","Manager"));        empList.add(new Employee(3, "Dave",45,"Male","Support"));        String fileName = "employees.xml";        writeFileUsingJDOM(empList, fileName);    }    private static void writeFileUsingJDOM(List empList, String fileName) throws IOException {        Document doc = new Document();        doc.setRootElement(new Element("Employees",  Namespace.getNamespace("http://www.journaldev.com/employees")));        for(Employee emp : empList){            Element employee = new Element("Employee");            employee.setAttribute("id",""+emp.getId());            employee.addContent(new Element("age").setText(""+emp.getAge()));            employee.addContent(new Element("name").setText(emp.getName()));            employee.addContent(new Element("gender").setText(emp.getGender()));            employee.addContent(new Element("role").setText(emp.getRole()));            doc.getRootElement().addContent(employee);        }        //JDOM document is ready now, lets write it to file now        XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());        //output xml to console for debugging        //xmlOutputter.output(doc, System.out);        xmlOutputter.output(doc, new FileOutputStream(fileName));    }}

運行程式將獲得下面的XML檔案:
employees.xml

    25    Pankaj    Male    Java Developer        34    Mona    Female    Manager       45    Dave    Male    Support

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.