瘋狂

          STANDING ON THE SHOULDERS OF GIANTS
          posts - 481, comments - 486, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          日歷

          <2010年1月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          公告

          公眾號:
          QQjava交流群:51374316



          相冊

          搜索

          •  

          積分與排名

          • 積分 - 2844360
          • 排名 - 2

          最新隨筆

          XStream 官方例子

          Posted on 2010-01-19 16:54 瘋狂 閱讀(985) 評論(0)  編輯  收藏

          Two Minute Tutorial

          This is a very quick introduction to XStream. Skim read it to get an idea of how simple it is to convert objects to XML and back again. I'm sure you'll have questions afterwards.

          Create classes to be serialized

          Here's a couple of simple classes. XStream can convert instances of these to XML and back again.

          public class Person {
          private String firstname;
          private String lastname;
          private PhoneNumber phone;
          private PhoneNumber fax;
          // ... constructors and methods
          }
          public class PhoneNumber {
          private int code;
          private String number;
          // ... constructors and methods
          }

          Note: Notice that the fields are private. XStream doesn't care about the visibility of the fields. No getters or setters are needed. Also, XStream does not limit you to having a default constructor.

          Initializing XStream

          To use XStream, simply instantiate the XStream class:

          XStream xstream = new XStream();

          You require xstream-[version].jar and xpp3-[version].jar in the classpath. XPP3 is a very fast XML pull-parser implementation. If you do not want to include this dependency, you can use a standard JAXP DOM parser instead:

          XStream xstream = new XStream(new DomDriver()); // does not require XPP3 library

          Note: This class is a simple facade designed for common operations. For more flexibility you may choose to create your own facade that behaves differently.

          Now, to make the XML outputted by XStream more concise, you can create aliases for your custom class names to XML element names. This is the only type of mapping required to use XStream and even this is optional.

          xstream.alias("person", Person.class);
          xstream.alias("phonenumber", PhoneNumber.class);

          Note: This is an optional step. Without it XStream would work fine, but the XML element names would contain the fully qualified name of each class (including package) which would bulk up the XML a bit. See the Alias Tutorial a more complete introduction.

          Serializing an object to XML

          Let's create an instance of Person and populate its fields:

          Person joe = new Person("Joe", "Walnes");
          joe.setPhone(new PhoneNumber(123, "1234-456"));
          joe.setFax(new PhoneNumber(123, "9999-999"));

          Now, to convert it to XML, all you have to do is make a simple call to XStream:

          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>

          It's that simple. Look at how clean the XML is.

          Deserializing an object back from XML

          To reconstruct an object, purely from the XML:

          Person newJoe = (Person)xstream.fromXML(xml);

          And that's how simple XStream is!

          Summary

          To recap:

          • Create element name to class name aliases for any custom classes using xstream.alias(String elementName, Class cls);
          • Convert an object to XML using xstream.toXML(Object obj);
          • Convert XML back to an object using xstream.fromXML(String xml);

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 长治县| 宜州市| 太康县| 建湖县| 永修县| 石林| 东莞市| 瓮安县| 买车| 兴安县| 阿尔山市| 姚安县| 晋州市| 敦化市| 荔浦县| 蓝山县| 堆龙德庆县| 峡江县| 资溪县| 浦北县| 天门市| 大余县| 木兰县| 灵石县| 华亭县| 绵竹市| 建阳市| 赤城县| 三台县| 陈巴尔虎旗| 孝义市| 江华| 南雄市| 武隆县| 大丰市| 洪湖市| 甘德县| 巴林右旗| 连城县| 嘉兴市| 达州市|