biGpython

生亦何欢,死亦何苦? 予我長袖,我必善舞!

导航

SimpleXML详细用法(转)

之前曾写过一blog : XStream序列化JAVA对象为XML以及反序列化 (http://sjsky.iteye.com/blog/784434),今天介绍另一个Java Bean<->XML 之间序列化和反序列化的轻量级工具:Simple

 

官网:http://simple.sourceforge.net/home.php

截止目前最新版本(附近可下载):simple-xml-2.6.1.jar

 

特点:

  • jar lib文件只有360K左右的大小
  • 它的使用不需要依赖于其他 JAR 文件
  • 通过注解的方式,灵活方便

下面将分节详细介绍Simple的特点和使用方法:

  • [一]、简单bean的序列化和反序列化
  • [二]、自定义节点名称 
  • [三]、嵌套对象
  • [四]、可选的非强制性的元素或属性
  • [五]、List<Object>处理
  • [六]、inline 参数用法
  • [七]、构造函数的注解处理

 

[一]、简单bean的序列化和反序列化

 

      1.java bean

 

Java代码  收藏代码
  1. package michael.serialization.simplexml;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.simpleframework.xml.Attribute;  
  6. import org.simpleframework.xml.Element;  
  7. import org.simpleframework.xml.Root;  
  8.   
  9. /** 
  10.  *  
  11.  * @blog http://sjsky.iteye.com 
  12.  * @author Michael 
  13.  */  
  14. @Root  
  15. public class MyTestVo {  
  16.   
  17.     @Element  
  18.     private String userName;  
  19.   
  20.     @Attribute  
  21.     private String wife;  
  22.   
  23.     @Attribute  
  24.     private String realName;  
  25.   
  26.     @Element  
  27.     private Date bornDate;  
  28.   
  29.     @Element  
  30.     private Double height;  
  31.   
  32.     public String toString() {  
  33.         return "MyTestVo : [ userName = " + userName + " , wife = " + wife  
  34.                 + " , realName = " + realName + " , height = " + height  
  35.                 + " , bornDate = " + bornDate + " ]";  
  36.     }  
  37.     //省略set get等方法  
  38.     ......  
  39.   
  40. }  

      2.序列化

Java代码  收藏代码
  1. public static void main(String[] args) throws Exception {  
  2.         String xmlpath = "d:/test/michael/simple_testvo.xml";  
  3.   
  4.         MyTestVo vo = new MyTestVo();  
  5.         vo.setUserName("michael");  
  6.         vo.setRealName("大大");  
  7.         vo.setWife("小小");  
  8.         vo.setHeight(173.3d);  
  9.         vo.setBornDate(new Date());  
  10.   
  11.         try {  
  12.             Serializer serializer = new Persister();  
  13.             File result = new File(xmlpath);  
  14.             serializer.write(vo, result);  
  15.         } catch (Exception e) {  
  16.             e.printStackTrace();  
  17.         }  
  18.   
  19. }  

    序列化成功生成的simple_testvo.xml文件如下:

Xml代码  收藏代码
  1. <myTestVo wife="小小" realName="大大">  
  2.    <userName>michael</userName>  
  3.    <bornDate>2011-09-28 17:39:59.432 CST</bornDate>  
  4.    <height>173.3</height>  
  5. </myTestVo>  

   ps: 注解可以把Java的属性序列化时指定为属性或者节点元素

 

    3.反序列化

 

    把上述生成的XML文件反序列化成Java bean测试代码:

Java代码  收藏代码
  1.  public static void main(String[] args) throws Exception {  
  2.         String xmlpath = "d:/test/michael/simple_testvo.xml";  
  3.           
  4.         Serializer serializer = new Persister();  
  5.         File source = new File(xmlpath);  
  6.         try {  
  7.             MyTestVo vo = serializer.read(MyTestVo.class, source);  
  8.             System.out.println(vo);  
  9.         } catch (Exception e) {  
  10.             e.printStackTrace();  
  11.         }  
  12. }  

  如果XML中包括中文字符有可能反序列化时会报错,以utf-8的编码读取XML文件即可,故修改代码如下:

Java代码  收藏代码
  1. /** 
  2.     * @param args 
  3.     * @throws Exception 
  4.     */  
  5.    public static void main(String[] args) throws Exception {  
  6.        String xmlpath = "d:/test/michael/simple_testvo.xml";  
  7.   
  8.        Serializer serializer = new Persister();  
  9.   
  10.        try {  
  11.            InputStreamReader is = new InputStreamReader(new FileInputStream(  
  12.                    xmlpath), "utf-8");  
  13.            PropertyList parseVo = serializer.read(PropertyList.class, is);  
  14.            System.out.println(parseVo);  
  15.        } catch (Exception e) {  
  16.            e.printStackTrace();  
  17.        }  
  18.    }  

  运行反序列化,打印Java bean信息如下:

 
MyTestVo : [ userName = michael , wife = 小小小 , realName = 大大 , height = 173.3 , bornDate = Wed Sep 28 17:39:59 CST 2011 ]

 

[二]、自定义节点名称

      1.java bean

Java代码  收藏代码
  1. package michael.serialization.simplexml;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.simpleframework.xml.Attribute;  
  6. import org.simpleframework.xml.Element;  
  7. import org.simpleframework.xml.Root;  
  8.   
  9. /** 
  10.  * @blog http://sjsky.iteye.com 
  11.  * @author Michael 
  12.  */  
  13. @Root(name = "MyTest")  
  14. public class MyTestVo {  
  15.   
  16.     @Element  
  17.     private String userName;  
  18.   
  19.     @Attribute(name = "MyWife")  
  20.     private String wife;  
  21.   
  22.     @Attribute  
  23.     private String realName;  
  24.   
  25.     @Element(name = "born")  
  26.     private Date bornDate;  
  27.   
  28.     @Element  
  29.     private Double height;  
  30.   
  31.     @Override  
  32.     public String toString() {  
  33.         return "MyTestVo : [ userName = " + userName + " , wife = " + wife  
  34.                 + " , realName = " + realName + " , height = " + height  
  35.                 + " , bornDate = " + bornDate + " ]";  
  36.     }  
  37.     //set get ......  
  38. }  

      2.序列化

 

   序列化后生成的simple_testvo.xml文件如下:

 

Xml代码  收藏代码
  1. <MyTest MyWife="小小" realName="大大">  
  2.    <userName>michael</userName>  
  3.    <born>2011-09-28 21:47:37.455 CST</born>  
  4.    <height>173.3</height>  
  5. </MyTest>  

   可以和之前的序列化XML文件对比下,看看区别在哪里。

 

      3.反序列化

 

       运行反序列化程序后的打印结果如下:

MyTestVo : [ userName = michael , wife = 小小 , realName = 大大 , height = 173.3 , bornDate = Wed Sep 28 21:47:37 CST 2011 ]

 

[三]、嵌套对象

 

      1.java bean

 

Java代码  收藏代码
  1. package michael.serialization.simplexml;  
  2.   
  3. import org.simpleframework.xml.Attribute;  
  4. import org.simpleframework.xml.Element;  
  5. import org.simpleframework.xml.Root;  
  6.   
  7. /** 
  8.  * @blog http://sjsky.iteye.com 
  9.  * @author Michael 
  10.  */  
  11. @Root  
  12. public class ConfigurationVo {  
  13.     @Element  
  14.     private ServerVo server;  
  15.   
  16.     @Attribute  
  17.     private int id;  
  18.   
  19.     public ServerVo getServer() {  
  20.         return server;  
  21.     }  
  22.   
  23.     public int getId() {  
  24.         return id;  
  25.     }  
  26.   
  27.     public void setServer(ServerVo pServer) {  
  28.         server = pServer;  
  29.     }  
  30.   
  31.     public void setId(int pId) {  
  32.         id = pId;  
  33.     }  
  34.   
  35. }  

 

Java代码  收藏代码
  1. package michael.serialization.simplexml;  
  2.   
  3. import org.simpleframework.xml.Attribute;  
  4. import org.simpleframework.xml.Element;  
  5. import org.simpleframework.xml.Root;  
  6. /** 
  7.  * @blog http://sjsky.iteye.com 
  8.  * @author Michael 
  9.  */  
  10. @Root  
  11. public class ServerVo {  
  12.     @Attribute  
  13.     private int port;  
  14.   
  15.     @Element  
  16.     private String host;  
  17.   
  18.     @Element  
  19.     private SecurityVo security;  
  20.   
  21.     public int getPort() {  
  22.         return port;  
  23.     }  
  24.   
  25.     public String getHost() {  
  26.         return host;  
  27.     }  
  28.   
  29.     public SecurityVo getSecurity() {  
  30.         return security;  
  31.     }  
  32.   
  33.     public void setPort(int pPort) {  
  34.         port = pPort;  
  35.     }  
  36.   
  37.     public void setHost(String pHost) {  
  38.         host = pHost;  
  39.     }  
  40.   
  41.     public void setSecurity(SecurityVo pSecurity) {  
  42.         security = pSecurity;  
  43.     }  
  44.   
  45. }  

 

Java代码  收藏代码
  1. package michael.serialization.simplexml;  
  2.   
  3. import org.simpleframework.xml.Attribute;  
  4. import org.simpleframework.xml.Element;  
  5. import org.simpleframework.xml.Root;  
  6.   
  7. /** 
  8.  * @blog http://sjsky.iteye.com 
  9.  * @author Michael 
  10.  */  
  11. @Root  
  12. public class SecurityVo {  
  13.     @Attribute  
  14.     private boolean ssl;  
  15.   
  16.     @Element  
  17.     private String keyStore;  
  18.   
  19.     public boolean isSsl() {  
  20.         return ssl;  
  21.     }  
  22.   
  23.     public String getKeyStore() {  
  24.         return keyStore;  
  25.     }  
  26.   
  27.     public void setSsl(boolean pSsl) {  
  28.         ssl = pSsl;  
  29.     }  
  30.   
  31.     public void setKeyStore(String pKeyStore) {  
  32.         keyStore = pKeyStore;  
  33.     }  
  34.   
  35. }  

 

      2.序列化

 

Java代码  收藏代码
  1. /** 
  2.      * @param args 
  3.      * @throws Exception 
  4.      */  
  5.     public static void main(String[] args) throws Exception {  
  6.         String xmlpath = "d:/test/michael/simple_testvo.xml";  
  7.   
  8.         SecurityVo security = new SecurityVo();  
  9.         security.setSsl(true);  
  10.         security.setKeyStore("Michael");  
  11.   
  12.         ServerVo server = new ServerVo();  
  13.         server.setHost("sjsky.iteye.com");  
  14.         server.setPort(8088);  
  15.         server.setSecurity(security);  
  16.   
  17.         ConfigurationVo config = new ConfigurationVo();  
  18.         config.setId(10000);  
  19.         config.setServer(server);  
  20.   
  21.         Serializer serializer = new Persister();  
  22.         try {  
  23.             File xmlFile = new File(xmlpath);  
  24.             serializer.write(config, xmlFile);  
  25.         } catch (Exception e) {  
  26.             e.printStackTrace();  
  27.         }  
  28. }  

   运行上述方法,序列化生成的XML文件如下:

 

Xml代码  收藏代码
  1. <configurationVo id="10000">  
  2.    <server port="8088">  
  3.       <host>sjsky.iteye.com</host>  
  4.       <security ssl="true">  
  5.          <keyStore>Michael</keyStore>  
  6.       </security>  
  7.    </server>  
  8. </configurationVo>  

 

      3.反序列化的方法和之前的一致,自己 可以 测试下结果是否正确。

 

[四]、可选的非强制性的元素或属性

 

      1.java bean

Java代码  收藏代码
  1. package michael.serialization.simplexml;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.simpleframework.xml.Attribute;  
  6. import org.simpleframework.xml.Element;  
  7. import org.simpleframework.xml.Root;  
  8.   
  9. /** 
  10.  * @blog http://sjsky.iteye.com 
  11.  * @author Michael 
  12.  */  
  13. @Root  
  14. public class MyTestVo {  
  15.   
  16.     @Element  
  17.     private String userName;  
  18.   
  19.     // 不是每个人都有妻子的 吼吼  
  20.     @Attribute(required = false)  
  21.     private String wife;  
  22.   
  23.     @Attribute  
  24.     private String realName;  
  25.   
  26.     // 不想泄露年龄噢  
  27.     @Element(required = false)  
  28.     private Date bornDate;  
  29.   
  30.     @Element  
  31.     private Double height;  
  32.   
  33.     @Override  
  34.     public String toString() {  
  35.         return "MyTestVo : [ userName = " + userName + " , wife = " + wife  
  36.                 + " , realName = " + realName + " , height = " + height  
  37.                 + " , bornDate = " + bornDate + " ]";  
  38.     }  
  39.   
  40.    //省略setter getter方法  
  41.   
  42. }  

 

      2.序列化

Java代码  收藏代码
  1. /** 
  2.     * @param args 
  3.     * @throws Exception 
  4.     */  
  5.    public static void main(String[] args) throws Exception {  
  6.        String xmlpath = "d:/test/michael/simple_testvo.xml";  
  7.   
  8.        MyTestVo vo = new MyTestVo();  
  9.        vo.setUserName("michael");  
  10.        vo.setRealName("大大");  
  11.        vo.setHeight(173.3d);  
  12.   
  13.        Serializer serializer = new Persister();  
  14.        try {  
  15.            File xmlFile = new File(xmlpath);  
  16.            serializer.write(vo, xmlFile);  
  17.        } catch (Exception e) {  
  18.            e.printStackTrace();  
  19.        }  

   运行序列化程序后生成的XML文件如下:

 

Xml代码  收藏代码
  1. <myTestVo realName="大大">  
  2.    <userName>michael</userName>  
  3.    <height>173.3</height>  
  4. </myTestVo>  

      3.反序列化

 

     运行反序列化程序后打印结果如下:

 

MyTestVo : [ userName = michael , wife = null , realName = 大大 , height = 173.3 , bornDate = null ]

 

[五]、List<Object>处理

      1.java bean

Java代码  收藏代码
  1. package michael.serialization.simplexml;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.InputStreamReader;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import org.simpleframework.xml.Attribute;  
  9. import org.simpleframework.xml.ElementList;  
  10. import org.simpleframework.xml.Root;  
  11. import org.simpleframework.xml.Serializer;  
  12. import org.simpleframework.xml.core.Persister;  
  13.   
  14. /** 
  15.  * @blog http://sjsky.iteye.com 
  16.  * @author Michael 
  17.  */  
  18. @Root  
  19. public class PropertyList {  
  20.   
  21.     @ElementList  
  22.     private List<EntryVo> list;  
  23.   
  24.     @Attribute  
  25.     private String name;  
  26.   
  27.     public List<EntryVo> getList() {  
  28.         return list;  
  29.     }  
  30.   
  31.     public String getName() {  
  32.         return name;  
  33.     }  
  34.   
  35.     public void setList(List<EntryVo> pList) {  
  36.         list = pList;  
  37.     }  
  38.   
  39.     public void setName(String pName) {  
  40.         name = pName;  
  41.     }  
  42.   
  43.     @Override  
  44.     public String toString() {  
  45.         return "PropertyList : [ name = " + name + " , EntryVo list size = "  
  46.                 + list.size() + " ] .";  
  47.     }  
  48. }  

 

Java代码  收藏代码
  1. package michael.serialization.simplexml;  
  2.   
  3. import org.simpleframework.xml.Attribute;  
  4. import org.simpleframework.xml.Element;  
  5. import org.simpleframework.xml.Root;  
  6.   
  7. /** 
  8.  * @blog http://sjsky.iteye.com 
  9.  * @author Michael 
  10.  */  
  11. @Root  
  12. public class EntryVo {  
  13.   
  14.     @Attribute  
  15.     private String name;  
  16.   
  17.     @Element  
  18.     private String value;  
  19.   
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.   
  24.     public String getValue() {  
  25.         return value;  
  26.     }  
  27.   
  28.     public void setName(String pName) {  
  29.         name = pName;  
  30.     }  
  31.   
  32.     public void setValue(String pValue) {  
  33.         value = pValue;  
  34.     }  
  35.   
  36. }  

 

      2.序列化

Java代码  收藏代码
  1. /** 
  2.     * @param args 
  3.     * @throws Exception 
  4.     */  
  5.    public static void main(String[] args) throws Exception {  
  6.        String xmlpath = "d:/test/michael/simple_testvo.xml";  
  7.   
  8.        Serializer serializer = new Persister();  
  9.   
  10.        try {  
  11.            PropertyList vo = initBean();  
  12.            serializer.write(vo, new File(xmlpath));  
  13.        } catch (Exception e) {  
  14.            e.printStackTrace();  
  15.        }  
  16.    }  
  17.   
  18.    private static PropertyList initBean() {  
  19.        PropertyList vo = new PropertyList();  
  20.        vo.setName("Wife List");  
  21.        List<EntryVo> subList = new ArrayList<EntryVo>();  
  22.        EntryVo subvo = new EntryVo();  
  23.        subvo.setName("A");  
  24.        subvo.setValue("福晋");  
  25.        subList.add(subvo);  
  26.        subvo = new EntryVo();  
  27.        subvo.setName("B");  
  28.        subvo.setValue("侧福晋");  
  29.        subList.add(subvo);  
  30.        subvo = new EntryVo();  
  31.        subvo.setName("C");  
  32.        subvo.setValue("小三");  
  33.        subList.add(subvo);  
  34.        subvo = new EntryVo();  
  35.        subvo.setName("D");  
  36.        subvo.setValue("二奶");  
  37.        subList.add(subvo);  
  38.        vo.setList(subList);  
  39.        return vo;  
  40.   
  41.    }  

 

 运行序列化程序后生成的XML文件如下:

 

Xml代码  收藏代码
  1. <propertyList name="Wife List">  
  2.    <list class="java.util.ArrayList">  
  3.       <entryVo name="A">  
  4.          <value>福晋</value>  
  5.       </entryVo>  
  6.       <entryVo name="B">  
  7.          <value>侧福晋</value>  
  8.       </entryVo>  
  9.       <entryVo name="C">  
  10.          <value>小三</value>  
  11.       </entryVo>  
  12.       <entryVo name="D">  
  13.          <value>二奶</value>  
  14.       </entryVo>  
  15.    </list>  
  16. </propertyList>  

 

      3.反序列化,运行结果打印对象信息如下:

 

PropertyList : [ name = Wife List , EntryVo list size = 4 ] .

      4.修改注解@ElementList的参数

 

Java代码  收藏代码
  1. @ElementList(name = "WifeList", entry = "wife")  
  2. private List<EntryVo> list;  

    序列化后生成的XML文件如下:

 

Xml代码  收藏代码
  1. <propertyList name="Wife List">  
  2.    <WifeList class="java.util.ArrayList">  
  3.       <wife name="A">  
  4.          <value>福晋</value>  
  5.       </wife>  
  6.       <wife name="B">  
  7.          <value>侧福晋</value>  
  8.       </wife>  
  9.       <wife name="C">  
  10.          <value>小三</value>  
  11.       </wife>  
  12.       <wife name="D">  
  13.          <value>二奶</value>  
  14.       </wife>  
  15.    </WifeList>  
  16. </propertyList>  

    注意XML文件的变化。

 

[六]、 inline 参数用法

      1.java bean

       以上节中得bean为基础修改注解如下:

Java代码  收藏代码
  1. @Root  
  2. public class PropertyList {  
  3.   
  4.     @ElementList(name = "WifeList", entry = "wife", inline = true)  
  5.     private List<EntryVo> list;  
  6.   
  7.     @Attribute  
  8.     private String name;  
  9.   
  10.     public List<EntryVo> getList() {  
  11.         return list;  
  12.     }  
  13.   
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.   
  18.     public void setList(List<EntryVo> pList) {  
  19.         list = pList;  
  20.     }  
  21.   
  22.     public void setName(String pName) {  
  23.         name = pName;  
  24.     }  
  25.   
  26.     @Override  
  27.     public String toString() {  
  28.         return "PropertyList : [ name = " + name + " , EntryVo list size = "  
  29.                 + list.size() + " ] .";  
  30.     }  
  31. }  

 

      2.序列化后生成的XML文件如下:

 

Java代码  收藏代码
  1. <propertyList name="Wife List">  
  2.    <wife name="A">  
  3.       <value>福晋</value>  
  4.    </wife>  
  5.    <wife name="B">  
  6.       <value>侧福晋</value>  
  7.    </wife>  
  8.    <wife name="C">  
  9.       <value>小三</value>  
  10.    </wife>  
  11.    <wife name="D">  
  12.       <value>二奶</value>  
  13.    </wife>  
  14. </propertyList>  

    和上节生成的文件相比,XML结构少了一个层次。

 

[七]、构造函数的注解处理

      1.java bean

 

Java代码  收藏代码
  1. package michael.serialization.simplexml;  
  2.   
  3. import org.simpleframework.xml.Attribute;  
  4. import org.simpleframework.xml.Element;  
  5. import org.simpleframework.xml.Root;  
  6. import org.simpleframework.xml.Serializer;  
  7. import org.simpleframework.xml.core.Persister;  
  8.   
  9. /** 
  10.  * @blog http://sjsky.iteye.com 
  11.  * @author Michael 
  12.  */  
  13. @Root  
  14. public class EntryVo {  
  15.     public EntryVo(@Attribute(name = "name")  
  16.     String name, @Element(name = "value")  
  17.     String value) {  
  18.         this.name = name;  
  19.         this.value = value;  
  20.     }  
  21.   
  22.     @Attribute(name = "name")  
  23.     private String name;  
  24.   
  25.     @Element(name = "value")  
  26.     private String value;  
  27.   
  28.     public String getName() {  
  29.         return name;  
  30.     }  
  31.   
  32.     public String getValue() {  
  33.         return value;  
  34.     }  
  35.   
  36.     public void setName(String pName) {  
  37.         name = pName;  
  38.     }  
  39.   
  40.     public void setValue(String pValue) {  
  41.         value = pValue;  
  42.     }  
  43.   
  44.     @Override  
  45.     public String toString() {  
  46.         return "EntryVo : [ name = " + name + ", value = " + value + " ].";  
  47.     }  
  48. }  

 

      2.序列化

 

      生成的XML文件如下:

<entryVo name="blog">
<value>http://sjsky.iteye.com</value>
</entryVo>

 

      3.反序列化

 

      反序列化生成的bean的信息打印如下:

 

EntryVo : [ name = blog, value = http://sjsky.iteye.com ].

 

ps:如果java bean有参数的构函数,需要在构造函数的参数前也加上相应的注解,否则在反序列化时会出错。

 

本文就先介绍到这,下次再介绍其他运用事例。

 

 

本文连接:http://sjsky.iteye.com/blog/1182057

 

 

转载请注明来自:Michael's blog @ http://sjsky.iteye.com 

posted on 2012-02-07 11:20  biGpython  阅读(2938)  评论(0编辑  收藏  举报