(Tips)Java对象的XML序列化和反序列化 - (1)

XML是一种标准的数据交换规范,可以方便地用于在应用之间交换各类数据。如果能在Java对象和XML文档之间建立某种映射,例如Java对象的XML序列化和反序列化,那么就可以使Java的对象方便地与其他应用进行交换。

java.beans包里面有两个类XMLEncoder和Decoder,分别用于将符合JabaBeans规范的Java对象以XML方式序列化和反序列化。以下代码显示了如何使用这两个类实现Java对象的XML编码和解码。

待序列化的Java类:

[java]  view plain copy
  1. import java.io.Serializable;  
  2.   
  3. public class SerialableObject implements Serializable  
  4. {  
  5.    private static final long serialVersionUID = 8745578444312339136L;  
  6.   
  7.    public SerialableObject()  
  8.    {  
  9.    }  
  10.   
  11.    public SerialableObject(int id, String name, double value)  
  12.    {  
  13.       this.id = id;  
  14.       this.name = name;  
  15.       this.value = value;  
  16.    }  
  17.   
  18.    public int getId()  
  19.    {  
  20.       return id;  
  21.    }  
  22.   
  23.    public void setId(int id)  
  24.    {  
  25.       this.id = id;  
  26.    }  
  27.   
  28.    public String getName()  
  29.    {  
  30.       return name;  
  31.    }  
  32.   
  33.    public void setName(String name)  
  34.    {  
  35.       this.name = name;  
  36.    }  
  37.   
  38.    public double getValue()  
  39.    {  
  40.       return value;  
  41.    }  
  42.   
  43.    public void setValue(double value)  
  44.    {  
  45.       this.value = value;  
  46.    }  
  47.   
  48.    private int id;  
  49.    private String name;  
  50.    private double value;  
  51.   
  52. }  

XML序列化和反序列化用法演示类:

[java]  view plain copy
  1. import java.beans.XMLDecoder;  
  2. import java.beans.XMLEncoder;  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.OutputStream;  
  10. import java.util.List;  
  11. import java.util.Vector;  
  12.   
  13. public class XmlSerialize  
  14. {  
  15.    public XmlSerialize()  
  16.    {  
  17.    }  
  18.   
  19.    public void serializeSingleObject(OutputStream os, Object obj)       // 序列化单个java对象  
  20.    {  
  21.       // XMLEncoder xe = new XMLEncoder(os);  
  22.       XMLEncoder xe = new XMLEncoder(os, "GBK"true0);     // 仅用于Java SE 7  
  23.       xe.writeObject(obj);    // 序列化成XML字符串  
  24.       xe.close();  
  25.    }  
  26.   
  27.    public Object deserializeSingleObject(InputStream is)       // 反序列化单个Java对象  
  28.    {  
  29.       XMLDecoder xd = new XMLDecoder(is);  
  30.       Object obj = xd.readObject();       // 从XML序列中解码为Java对象  
  31.       xd.close();  
  32.       return obj;  
  33.    }  
  34.   
  35.    public void serializeMultipleObject(OutputStream os, List<Object> objs)       // 序列化多个Java对象  
  36.    {  
  37.       XMLEncoder xe = new XMLEncoder(os);  
  38.       xe.writeObject(objs);    // 序列化成XML字符串  
  39.       xe.close();  
  40.    }  
  41.   
  42.    public List<Object> deserializeMultipleObject(InputStream is)        // 反序列化多个Java对象  
  43.    {  
  44.       XMLDecoder xd = new XMLDecoder(is);  
  45.       @SuppressWarnings("unchecked")  
  46.       List<Object> objs = (List<Object>)xd.readObject();       // 从XML序列中解码为Java对象列表  
  47.       xd.close();  
  48.       return objs;  
  49.    }  
  50.   
  51.    public void runSingleObject()  
  52.    {  
  53.       File xmlFile = new File("object.xml");  
  54.   
  55.       SerialableObject jo4Out = new SerialableObject(1"Java序列化为XML"3.14159265359);     // 创建待序列化的对象  
  56.       try  
  57.       {  
  58.          FileOutputStream ofs = new FileOutputStream(xmlFile);       // 创建文件输出流对象  
  59.          serializeSingleObject(ofs, jo4Out);  
  60.          ofs.close();  
  61.       } catch (FileNotFoundException e)  
  62.       {  
  63.          e.printStackTrace();  
  64.       } catch (IOException e)  
  65.       {  
  66.          e.printStackTrace();  
  67.       }  
  68.   
  69.       try  
  70.       {  
  71.          FileInputStream ifs = new FileInputStream(xmlFile);  
  72.          SerialableObject jo4In = (SerialableObject)deserializeSingleObject(ifs);  
  73.          System.out.println("id: " + jo4In.getId());  
  74.          System.out.println("name: " + jo4In.getName());  
  75.          System.out.println("value: " + jo4In.getValue());  
  76.       } catch (FileNotFoundException e)  
  77.       {  
  78.          e.printStackTrace();  
  79.       }  
  80.    }  
  81.   
  82.    public void runMultipleObject()  
  83.    {  
  84.       File xmlFile = new File("objects.xml");  
  85.   
  86.       List<SerialableObject> sos4Out = new Vector<SerialableObject>();  
  87.       sos4Out.add(new SerialableObject(1"Java序列化为XML - 1"3.14));     // 创建待序列化的对象  
  88.       sos4Out.add(new SerialableObject(2"Java序列化为XML - 2"3.14159));     // 创建待序列化的对象  
  89.       sos4Out.add(new SerialableObject(3"Java序列化为XML - 3"3.1415926));     // 创建待序列化的对象  
  90.       sos4Out.add(new SerialableObject(4"Java序列化为XML - 4"3.141592653));     // 创建待序列化的对象  
  91.       sos4Out.add(new SerialableObject(5"Java序列化为XML - 5"3.14159265359));     // 创建待序列化的对象  
  92.   
  93.       try  
  94.       {  
  95.          FileOutputStream ofs = new FileOutputStream(xmlFile);       // 创建文件输出流对象  
  96.          serializeSingleObject(ofs, sos4Out);  
  97.          ofs.close();  
  98.       } catch (FileNotFoundException e)  
  99.       {  
  100.          e.printStackTrace();  
  101.       } catch (IOException e)  
  102.       {  
  103.          e.printStackTrace();  
  104.       }  
  105.   
  106.       try  
  107.       {  
  108.          FileInputStream ifs = new FileInputStream(xmlFile);  
  109.          @SuppressWarnings("unchecked")  
  110.          List<SerialableObject> sos4In = (List<SerialableObject>)deserializeSingleObject(ifs);  
  111.          for(SerialableObject jo4In : sos4In)  
  112.          {  
  113.             System.out.println("id: " + jo4In.getId());  
  114.             System.out.println("name: " + jo4In.getName());  
  115.             System.out.println("value: " + jo4In.getValue());  
  116.          }  
  117.       } catch (FileNotFoundException e)  
  118.       {  
  119.          e.printStackTrace();  
  120.       }  
  121.    }  
  122.   
  123.    public static void main(String[] args)  
  124.    {  
  125.       XmlSerialize xs = new XmlSerialize();  
  126.       xs.runSingleObject();  
  127.       xs.runMultipleObject();  
  128.    }  
  129.   
  130. }  

需要注意的是,待序列化的类必须要符合JavaBeans的格式规范,即:具有一个无参的public构造函数,所有数据成员的访问均采用getter/setter模式,此外,这个类必须是public的,并且实现了java.io.Serializable接口。

程序运行之后,会产生两个文件:

object.xml是runSingleObject方法生成的,存放了单个的SerialableObject的值:

[html]  view plain copy
  1. <?xml version="1.0" encoding="GBK"?>  
  2. <java version="1.7.0" class="java.beans.XMLDecoder">  
  3.  <object class="SerialableObject">  
  4.   <void property="id">  
  5.    <int>1</int>  
  6.   </void>  
  7.   <void property="name">  
  8.    <string>Java序列化为XML</string>  
  9.   </void>  
  10.   <void property="value">  
  11.    <double>3.14159265359</double>  
  12.   </void>  
  13.  </object>  
  14. </java>   

objects.xml是runMultipleObject方法产生的,存放了5个SerializableObject的值:

[html]  view plain copy
  1. <?xml version="1.0" encoding="GBK"?>  
  2. <java version="1.7.0" class="java.beans.XMLDecoder">  
  3.  <object class="java.util.Vector">  
  4.   <void method="add">  
  5.    <object class="SerialableObject">  
  6.     <void property="id">  
  7.      <int>1</int>  
  8.     </void>  
  9.     <void property="name">  
  10.      <string>Java序列化为XML - 1</string>  
  11.     </void>  
  12.     <void property="value">  
  13.      <double>3.14</double>  
  14.     </void>  
  15.    </object>  
  16.   </void>  
  17.   <void method="add">  
  18.    <object class="SerialableObject">  
  19.     <void property="id">  
  20.      <int>2</int>  
  21.     </void>  
  22.     <void property="name">  
  23.      <string>Java序列化为XML - 2</string>  
  24.     </void>  
  25.     <void property="value">  
  26.      <double>3.14159</double>  
  27.     </void>  
  28.    </object>  
  29.   </void>  
  30.   <void method="add">  
  31.    <object class="SerialableObject">  
  32.     <void property="id">  
  33.      <int>3</int>  
  34.     </void>  
  35.     <void property="name">  
  36.      <string>Java序列化为XML - 3</string>  
  37.     </void>  
  38.     <void property="value">  
  39.      <double>3.1415926</double>  
  40.     </void>  
  41.    </object>  
  42.   </void>  
  43.   <void method="add">  
  44.    <object class="SerialableObject">  
  45.     <void property="id">  
  46.      <int>4</int>  
  47.     </void>  
  48.     <void property="name">  
  49.      <string>Java序列化为XML - 4</string>  
  50.     </void>  
  51.     <void property="value">  
  52.      <double>3.141592653</double>  
  53.     </void>  
  54.    </object>  
  55.   </void>  
  56.   <void method="add">  
  57.    <object class="SerialableObject">  
  58.     <void property="id">  
  59.      <int>5</int>  
  60.     </void>  
  61.     <void property="name">  
  62.      <string>Java序列化为XML - 5</string>  
  63.     </void>  
  64.     <void property="value">  
  65.      <double>3.14159265359</double>  
  66.     </void>  
  67.    </object>  
  68.   </void>  
  69.  </object>  
  70. </java>   
发布了40 篇原创文章 · 获赞 17 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/rendawei636/article/details/42103157