---
title: "根据对象生成XML文档"
author: "freeroy"
date: "2011-07-15T11:03:58Z"
tags: []
categories: ["编程语言"]
slug: "xml"
draft: false
lastmod: "2011-07-15T11:03:58Z"
id: 1125047
url: "https://freeroy.iteye.com/blog/1125047"
---

根据对象生成XML文档. 使用Java提供的java.beans.XMLEncoder  
和java.beans.XMLDecoder类

一、生成的xml文件如下：AStudent.xml

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;java version="1.5.0" class="java.beans.XMLDecoder"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;object class="StudentBean"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;void property="age"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;int&gt;15&lt;/int&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/void&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;void property="gender"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;string&gt;male&lt;/string&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/void&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;void property="name"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;string&gt;wamgwu&lt;/string&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/void&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;void property="phone"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;string&gt;55556666&lt;/string&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/void&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/object&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/java&gt;

二、描述学生的JavaBean

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * 描述学生的JavaBean  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class StudentBean ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 学生姓名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private String name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 学生性别  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private String gender;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 学生年龄  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private int age;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 学生电话号码  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private String phone;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public String toString()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) StringBuffer sb = new StringBuffer();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) sb.append("姓名：").append(this.name).append("； ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) sb.append("性别：").append(gender).append("； ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) sb.append("年龄：").append(age).append("； ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) sb.append("电话：").append(phone);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return sb.toString();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @return 返回 age。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public int getAge() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return age;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @param age 要设置的 age。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public void setAge(int age) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.age = age;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @return 返回 gender。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public String getGender() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return gender;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @param gender 要设置的 gender。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public void setGender(String gender) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.gender = gender;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @return 返回 name。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public String getName() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @param name 要设置的 name。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public void setName(String name) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.name = name;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @return 返回 phone。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public String getPhone() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return phone;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @param phone 要设置的 phone。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public void setPhone(String phone) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.phone = phone;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

三、主程序

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.beans.XMLDecoder;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.beans.XMLEncoder;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.BufferedInputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.BufferedOutputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.File;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.FileInputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.FileNotFoundException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.FileOutputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.IOException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * 根据对象生成XML文档.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * 使用Java提供的java.beans.XMLEncoder和java.beans.XMLDecoder类。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * 这是JDK 1.4以后才出现的类  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class Object2XML ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * 对象输出到XML文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @param obj 待输出的对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @param outFileName 目标XML文件的文件名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @return 返回输出XML文件的路径  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @throws FileNotFoundException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)public static String object2XML(Object obj, String outFileName)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)throws FileNotFoundException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 构造输出XML文件的字节输出流  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) File outFile = new File(outFileName);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFile));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 构造一个XML编码器  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) XMLEncoder xmlEncoder = new XMLEncoder(bos);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 使用XML编码器写对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) xmlEncoder.writeObject(obj);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 关闭编码器  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) xmlEncoder.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return outFile.getAbsolutePath();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * 把XML文件解码成对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @param inFileName 输入的XML文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @return 返回生成的对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) * @throws FileNotFoundException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)public static Object xml2Object(String inFileName)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)throws FileNotFoundException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 构造输入的XML文件的字节输入流  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inFileName));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 构造一个XML解码器  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) XMLDecoder xmlDecoder = new XMLDecoder(bis);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 使用XML解码器读对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Object obj = xmlDecoder.readObject();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 关闭解码器  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) xmlDecoder.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return obj;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)public static void main(String\[] args) throws IOException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 构造一个StudentBean对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) StudentBean student = new StudentBean();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) student.setName("wamgwu");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) student.setGender("male");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) student.setAge(15);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) student.setPhone("55556666");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 将StudentBean对象写到XML文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) String fileName = "AStudent.xml";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Object2XML.object2XML(student, fileName);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 从XML文件读StudentBean对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) StudentBean aStudent = (StudentBean)Object2XML.xml2Object(fileName);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)// 输出读到的对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) System.out.println(aStudent.toString());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)运行结果：  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)C:java&gt;java Object2XML姓名：wamgwu； 性别：male； 年龄：15； 电话：55556666