<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(948) 評論(0)  編輯  收藏 所屬分類: java 開發
    <2008年1月>
    303112345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    常用鏈接

    留言簿(3)

    隨筆分類

    隨筆檔案

    收藏夾

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 无码精品人妻一区二区三区免费| 亚洲成a人片在线不卡一二三区 | 亚洲AV无码专区在线观看成人| 亚洲精品国产成人片| 丰满妇女做a级毛片免费观看| 亚洲一区无码精品色| 最新亚洲人成网站在线观看| 一本久久综合亚洲鲁鲁五月天| 亚洲卡一卡2卡三卡4卡无卡三| 亚洲成人免费在线| 亚洲精品国产情侣av在线| 青青青国产在线观看免费网站 | 亚洲欭美日韩颜射在线二| 中文日本免费高清| 亚洲精品人成在线观看| 在线永久免费的视频草莓| 亚洲综合av一区二区三区不卡| 国产一区二区三区免费在线观看 | 美女又黄又免费的视频| 4338×亚洲全国最大色成网站| a毛片全部免费播放| 亚洲黄色一级毛片| 国产成人免费视频| 亚洲av日韩av综合| 免费一看一级毛片| 国产午夜成人免费看片无遮挡| 2022年亚洲午夜一区二区福利| 成年人网站在线免费观看| 三级片免费观看久久| 亚洲成熟xxxxx电影| 无码中文字幕av免费放| 人妻无码中文字幕免费视频蜜桃| 免费无码精品黄AV电影| 黄页网站在线视频免费| 亚洲成AV人片在线观看WWW| 成人黄18免费视频| 中文字幕无线码免费人妻| 亚洲无砖砖区免费| 亚洲另类少妇17p| 中文字幕无码免费久久99 | 最近的2019免费中文字幕|