Java&Xml tutorial (8) Convert Java objects to XML using JDOM

黄舟
Release: 2017-02-22 14:55:02
Original
1704 people have browsed it

In the previous tutorial, we learned how to use JDOM to parse and modify the content of XML files. This section describes how to convert Java objects into XML data and generate files.

JDOM’s Document class provides convenient methods to create elements and attributes. The XMLOutputter class can write Document to any OutputStream and Writer object.
In this example we create a collection of Employee objects and write it to an XML file.
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;
    }

}
Copy after login
Copy after login

We use the Employee object id as an attribute of the Employee element in the XML file, and set the namespace of the root element Employees to "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));
    }

}
Copy after login

Running the program will get the following XML file:
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>
Copy after login
Copy after login

Original address: http://www.php.cn/

In the previous tutorial, we learned how to use JDOM to parse and modify the content of XML files. This section introduces how to convert Java objects into XML data and generate files.
JDOM's Document class provides convenient methods to create elements and attributes. The XMLOutputter class can write Document to any OutputStream and Writer object.
In this example we create a collection of Employee objects and write it to an XML file.
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;
    }

}
Copy after login
Copy after login

We use the Employee object id as an attribute of the Employee element in the XML file, and set the namespace of the root element Employees to "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));
    }

}
Copy after login

Run the program and you will get the following XML file:
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>
Copy after login
Copy after login

The above is the content of Java&Xml tutorial (8) Using JDOM to convert Java objects into XML , for more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template