SimpleXML详细用法1(转)

[一]、简单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.反序列化的方法和之前的一致,自己 可以 测试下结果是否正确。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值