posts - 122,  comments - 25,  trackbacks - 0
          XML/JSON的操作類庫,codehaus的xstream,很簡單的在xml/json和java對象間轉換。

          1、所需jar包:xstream-[version].jarxpp3-[version].jar 。xpp3不是必須的,也可以用標準的JAXP DOM parser 來代替,如:
          XStream xstream = new XStream(new DomDriver()); // does not require XPP3 library

          另外,通過alias可實現xml屬性的支持。更多功能,請參閱 Tutorial 。

          2、XML轉換代碼片段:
          Class 轉換為 xml 的代碼片段:
          XStream xstream = new XStream();
          xstream.alias("person", Person.class);
          xstream.alias("phonenumber", PhoneNumber.class);
          Person joe = new Person("Joe""Walnes");
          joe.setPhone(
          new PhoneNumber(123"1234-456"));
          joe.setFax(
          new PhoneNumber(123"9999-999"));
          String xml 
          = xstream.toXML(joe);

          The resulting XML looks like this:
          <person>
            
          <firstname>Joe</firstname>
            
          <lastname>Walnes</lastname>
            
          <phone>
              
          <code>123</code>
              
          <number>1234-456</number>
            
          </phone>
            
          <fax>
              
          <code>123</code>
              
          <number>9999-999</number>
            
          </fax>
          </person>

          xml轉換為java類的代碼片段:
          Person newJoe = (Person)xstream.fromXML(xml);

          3、JSON
          另需jar包:jettison.jar 、stax-api.jar。JSON Turorial 。

          Write to json:

          package org.sensatic.json.test;

          import com.thoughtworks.xstream.XStream;
          import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

          public class WriteTest {
              
          public static void main(String[] args) {
                  Product product 
          = new Product("Banana""123"23.00);
                  XStream xstream 
          = new XStream(new JettisonMappedXmlDriver());
                  xstream.alias(
          "product", Product.class);
                  System.out.println(xstream.toXML(product));    
              }
          }

          Read from json:
          package org.sensatic.json.test;

          import com.thoughtworks.xstream.XStream;
          import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

          public class ReadTest {
              
          public static void main(String[] args) {
                  String json 
          = "{\"product\":{\"name\":\"Banana\",\"id\":\"123\""
                      
          + ",\"price\":\"23.0\"}}";
                      
                  XStream xstream 
          = new XStream(new JettisonMappedXmlDriver());
                  xstream.alias(
          "product", Product.class);
                  Product product 
          = (Product)xstream.fromXML(json);
                  System.out.println(product.getName());
              }
          }

          And that's how simple XStream is!
          posted on 2008-01-15 10:11 josson 閱讀(944) 評論(0)  編輯  收藏 所屬分類: java 開發
          <2008年1月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 敦化市| 苍南县| 霍城县| 丰顺县| 时尚| 玛纳斯县| 和田市| 桐柏县| 米林县| 社旗县| 东安县| 娱乐| 昌平区| 安庆市| 洪江市| 台南市| 怀来县| 长宁区| 松阳县| 天等县| 邵东县| 罗江县| 高要市| 南溪县| 平原县| 呼玛县| 安泽县| 宜川县| 咸宁市| 右玉县| 迁西县| 随州市| 康乐县| 商城县| 晋中市| 东乌珠穆沁旗| 监利县| 加查县| 正镶白旗| 桐庐县| 大埔县|