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)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 黔西县| 长泰县| 元朗区| 开阳县| 山东省| 灌云县| 娱乐| 玉溪市| 密山市| 东安县| 浙江省| 陕西省| 玉山县| 遂宁市| 扶沟县| 綦江县| 黑水县| 和政县| 渭源县| 盐池县| 芦山县| 封丘县| 邳州市| 清远市| 安溪县| 赣州市| 吕梁市| 昂仁县| 揭西县| 普兰县| 绥宁县| 邢台市| 隆子县| 龙井市| 沿河| 安陆市| 尖扎县| 汪清县| 桐庐县| 万宁市| 屯门区|