在前面的教程中我们学习了如何使用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.php.cn/”
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<Employee> 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<Employee> 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

<?xml version="1.0" encoding="UTF-8"?><Employees xmlns="http://www.journaldev.com/employees">  <Employee xmlns="" id="1">    <age>25</age>    <name>Pankaj</name>    <gender>Male</gender>    <role>Java Developer</role>  </Employee>  <Employee xmlns="" id="2">    <age>34</age>    <name>Mona</name>    <gender>Female</gender>    <role>Manager</role>  </Employee>  <Employee xmlns="" id="3">    <age>45</age>    <name>Dave</name>    <gender>Male</gender>    <role>Support</role>  </Employee></Employees>

原文地址:http://www.php.cn/

在前面的教程中我们学习了如何使用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.php.cn/”
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<Employee> 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<Employee> 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

<?xml version="1.0" encoding="UTF-8"?><Employees xmlns="http://www.journaldev.com/employees">  <Employee xmlns="" id="1">    <age>25</age>    <name>Pankaj</name>    <gender>Male</gender>    <role>Java Developer</role>  </Employee>  <Employee xmlns="" id="2">    <age>34</age>    <name>Mona</name>    <gender>Female</gender>    <role>Manager</role>  </Employee>  <Employee xmlns="" id="3">    <age>45</age>    <name>Dave</name>    <gender>Male</gender>    <role>Support</role>  </Employee></Employees>

以上就是Java&Xml教程(八)使用JDOM将Java对象转换为XML的内容,更多相关内容请关注PHP中文网(www.php.cn)!

更多相关文章

  1. Java&Xml教程(十)XML作为属性文件使用
  2. Java&Xml教程(十一)JAXB实现XML与Java对象转换
  3. Java对象、Json、Xml转换工具Jackson使用
  4. xml学习(1)xml的几种文件格式
  5. XML—XML文件约束之DTD详解
  6. xml学习(4) 创建xml 文件
  7. XML文件导入EXCEL
  8. 使用 XML 文件记录操作日志
  9. XMLTextReader和XmlDocument读取XML文件的比较

随机推荐

  1. Android显示圆角图片,可指定图片某几个角
  2. Android UI学习 - Tab的学习和使用
  3. iPhone相较Android机型所存在的四大硬伤
  4. android编译步骤
  5. Android 签名详解
  6. Android 查询天气源码分享
  7. 为Android内核添加hello world驱动并添加
  8. A06_RelativeLayout的属性设置
  9. Qt5.8开发Android:强制横屏
  10. android:imeOptions 控制软键盘右下角按