posts - 4,  comments - 2,  trackbacks - 0
          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          XStream是一個(gè)開源項(xiàng)目,一套簡單實(shí)用的類庫,用于序列化對象與XML對象之間的相互轉(zhuǎn)換。將XML文件內(nèi)容解析為一個(gè)對象或?qū)⒁粋€(gè)對象序列化為XML文件。

          XStream的相關(guān)信息可以到http://xstream.codehaus.org/下查看,它有專門的JavaDoc,可以方便的閱讀Xstream的函數(shù)及方法。

          ??????? 基本原理
          Xstream這個(gè)組件也提供了一個(gè)XML自省(introspection)機(jī)制用來把Java Bean映射成XML信息或者把XML信息映射成Java Bean。
          不同的是在解析xml信息時(shí)采用的是DOM方式

          ??????? 核心類,方法及步驟簡介
          Xstream在把xml信息和java bean相互轉(zhuǎn)換的過程中用到同一個(gè)應(yīng)用類。
          com.thoughtworks.xstream.XStream

          --------------------------------------------------------------
          常用的方法:
          ?XStream xstream = new XStream();
          ?fromXML();?用于從xml中將對象解析出來。
          ?toXML();?用于將對象序列化為xml文件。
          --------------------------------------------------------------

          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.
          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);

          ?


          Object Streams

          XStream provides alternative implementations of java.io.ObjectInputStream and java.io.ObjectOutputStream, allowing streams of objects to be serialized or deserialized from XML.

          This is useful when processing large sets of objects, as only one needs to be in memory at a time.
          Using the Object Streams

          The interface to the object streaming capabilities of XStream is through the standard java.io.ObjectOutputStream and java.io.ObjectInputStream objects.
          Example

          To serialize a stream of objects to XML:

          ObjectOutputStream out = xstream.createObjectOutputStream(someWriter);

          out.writeObject(new Person("Joe", "Walnes"));
          out.writeObject(new Person("Someone", "Else"));
          out.writeObject("hello");
          out.writeInt(12345);

          out.close();

          The resulting XML:

          <object-stream>
          ? <com.blah.Person>
          ??? <firstname>Joe</firstname>
          ??? <lastname>Walnes</lastname>
          ? </com.blah.Person>
          ? <com.blah.Person>
          ??? <firstname>Someone</firstname>
          ??? <lastname>Else</lastname>
          ? </com.blah.Person>
          ? <string>hello</string>
          ? <int>123</int>
          </object-stream>

          To deserialze the stream of objects from the XML:

          ObjectInputStream in = xstream.createObjectInputStream(someReader);

          Person a = (Person)in.readObject();
          Person b = (Person)in.readObject();
          String c = (String)in.readObject();
          int??? d = in.readInt();

          Considerations
          Root node

          Because an XML document can only have a single root node, all the serialized elements must be wrapped in an additional root node. This root node defaults to <object-stream>, as shown in the example above.

          This can be changed by using the overloaded method: xstream.createObjectOutputStream(Writer writer, String rootNodeName);
          Close the ObjectOutputStream

          Remember to call ObjectOutputStream.close(), otherwise the stream will contain incomplete XML.
          Detecting the end of the ObjectInputStream

          When there are no more objects left to read in the stream, ObjectInputStream.readObject() (or primitive equivalent) will throw java.io.EOFException.

          posted on 2006-12-30 14:34 ziwolf 閱讀(871) 評(píng)論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 汾阳市| 商都县| 灌云县| 拜城县| 恭城| 铁岭县| 且末县| 玉环县| 营山县| 永胜县| 济阳县| 县级市| 神农架林区| 合川市| 井陉县| 政和县| 南昌县| 西林县| 红河县| 五指山市| 大英县| 新安县| 石台县| 伊宁县| 灵山县| 张家港市| 兴隆县| 尼木县| 和龙市| 甘洛县| 原平市| 军事| 定日县| 大宁县| 华蓥市| 海晏县| 济南市| 文登市| 陆河县| 盐亭县| 西畴县|