SimpleXML详细用法2

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

 

      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有参数的构函数,需要在构造函数的参数前也加上相应的注解,否则在反序列化时会出错。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值