JDOM从对象编写XML文件示例

时间:2020-02-23 14:35:19  来源:igfitidea点击:

在上一教程中,我们学习了Java JDOM的基础知识以及如何将XML文件读取到Object。
在本教程中,我们将学习JDOM Write XML File示例。

JDOM写入XML文件

JDOM Document提供了轻松创建元素和属性的方法。
XMLOutputter类可用于将Document写入任何OutputStream或者Writer对象。

对于此示例,我们将创建一个Employee对象列表,然后将其写入XML文件。

package com.theitroad.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设置为Employee元素的属性,并设置根元素Employees的命名空间。

package com.theitroad.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.theitroad.xml.Employee;

public class JDOMXMLWriter {

  public static void main(String[] args) throws IOException {
      List<Employee> empList = new ArrayList<>();
      empList.add(new Employee(1, "hyman",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("https://www.theitroad.local/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));
  }

}

当我们在JDOM Write XML File示例程序之上运行时,我们得到以下XML文件。

<?xml version="1.0" encoding="UTF-8"?>
<Employees xmlns="https://www.theitroad.local/employees">
<Employee xmlns="" id="1">
  <age>25</age>
  <name>hyman</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>

请注意,Employee元素的名称空间为空,因为我们仅为根元素设置了名称空间。
如果要为所有元素使用相同的名称空间,则需要使用带有名称空间参数的构造函数专门创建它们。

还要检查" Format"类的使用以获得漂亮的打印XML文件。
我们也可以使用它以紧凑和原始格式输出XML。