xml转对象 jdom_JDOM从对象编写XML文件示例

xml转对象 jdom

In last tutorial we learned basics of Java JDOM and how we can read XML file to Object. In this tutorial we will learn about JDOM Write XML File example.

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

JDOM写入XML文件 (JDOM Write XML File)

JDOM Document provides methods to easily create elements and attributes. XMLOutputter class can be used to write the Document to any OutputStream or Writer object.

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

For this example, we will create a list of Employee object and then write it to XML file.

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

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;
    }
    
}

We will set Employee ID as attribute to Employee element and set the namespace for root element Employees.

我们将Employee ID设置为Employee元素的属性,并设置根元素Employees的命名空间。

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("https://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));
    }

}

When we run above JDOM Write XML File example program, we get following XML file.

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

<?xml version="1.0" encoding="UTF-8"?>
<Employees xmlns="https://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>

Notice that Employee elements have empty namespace because we set the namespace for root element only. If you want same namespace for all the elements, you need to create them specifically by using the constructor with namespace argument.

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

Also check the use of Format class for pretty printing XML file. We can use it to output XML in compact and raw format also.

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

翻译自: https://www.journaldev.com/1211/jdom-write-xml-file-example-object

xml转对象 jdom

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值