posts - 156,  comments - 601,  trackbacks - 0

          Simple是一個XML序列化框架,一個Java版本寬容的序列化框架,能夠快速在Java平臺上開發XML。支持通過annotations完全配置化的XML結構;提供版本管理框架允許向前和向后兼容序列化;更好的性能,使用輕量級StAX提升XML反序列化 進程,比XStream和JAXB更快;通過namespace annotations提供完全的命名空間支持;包括XML模板系統

          目前最新版本為 2.0.1. 官網地址:http://simple.sourceforge.net/home.php

          注:不過筆者認為,如果你使用JavaSE 6.0,則建議使用其自帶的JAXB(Java Architecture for XML Binding).其使用非常簡單,支持通過annotations完全配置化的XML結構。而且其也是Java中的一個規范實現, JAX-RS規范就使用JAXB來實現對象的序列化功能。


          下面是一個簡單示例:
          先定義一個Example對象, 我們只需要簡單的加上Annoation后,就可以進行對象與XML之間的轉換。
          @Root
          public class Example {

             @Element
             
          private String text;

             @Attribute
             
          private int index;

             
          public Example() {
                
          super();
             }  

             
          public Example(String text, int index) {
                
          this.text = text;
                
          this.index = index;
             }

             
          public String getMessage() {
                
          return text;
             }

             
          public int getId() {
                
          return index;
             }
          }

          進行XML序列化操作:
          Serializer serializer = new Persister();
          Example example 
          = new Example("Example message"123);
          File result 
          = new File("example.xml");

          serializer.write(example, result);

          下面是生成的XML文件內容:
          <example index="123">
             
          <text>Example message</text>
          </example>

          從XML文件中,反序列化后,得到Example對象實例:
          Serializer serializer = new Persister();
          File source 
          = new File("example.xml");

          Example example 
          = serializer.read(Example.class, source);

          到現在基本的演示完成,API使用起來非常簡單。
          查看更多示例(包括級聯對象的序列化與反序列化操作)

          Good Luck!
          Yours Matthew!

          posted on 2008-12-10 20:16 x.matthew 閱讀(3836) 評論(1)  編輯  收藏 所屬分類: Best Practise(JDK API)
          主站蜘蛛池模板: 临清市| 大关县| 许昌县| 晋州市| 宜州市| 永德县| 北京市| 木兰县| 炉霍县| 沾益县| 四子王旗| 江西省| 平利县| 沙河市| 调兵山市| 南安市| 茶陵县| 双流县| 金堂县| 方城县| 东山县| 清徐县| 峨山| 两当县| 黄石市| 泸溪县| 遂川县| 双江| 伊川县| 屯门区| 邵武市| 扶风县| 惠州市| 富宁县| 通州市| 新密市| 娄底市| 新龙县| 甘孜县| 阿拉尔市| 宽甸|