我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

如何使用JDOM将Java对象转换为XML

这篇文章运用简单易懂的例子给大家介绍如何使用JDOM将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 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
  

原文地址: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 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
  

关于如何使用JDOM将Java对象转换为XML就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


标题名称:如何使用JDOM将Java对象转换为XML
分享网址:http://mswzjz.cn/article/gsocgc.html

其他资讯