用 XStream 序列化/反序列化 XML 为 Java 对象
2007-06-05 18:12
841 查看
项目需要,懒得翻译了,反正很浅显,也很方便
http://xstream.codehaus.org/tutorial.html href="http://xstream.codehaus.org/index.html" target=_blank>
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.
You require
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.
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.
Now, to convert it to XML, all you have to do is make a simple call to XStream:
The resulting XML looks like this:
It's that simple. Look at how clean the XML is.
And that's how simple XStream is!
http://xstream.codehaus.org/tutorial.html href="http://xstream.codehaus.org/index.html" target=_blank>
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 theXStreamclass:
XStream xstream = new XStream();
You require
xstream-[version].jarand
xpp3-[version].jarin 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!
相关文章推荐
- 用 XStream 序列化/反序列化 XML 为 Java 对象(实例)
- 用 XStream 序列化/反序列化 XML 为 Java 对象(实例)
- 用 XStream 序列化/反序列化 XML 为 Java 对象(实例)
- Java对象的XML序列化和反序列化 - (1)
- Java对象的XML序列化和反序列化 - (6)
- 使用XStream把Java 对象序列化为XML!!!
- Java对象的XML序列化和反序列化 - (4)
- Java对象的XML序列化和反序列化 - (5)
- xstream ---java序列化到xml,xml反序列化到java对象(一)
- (Tips)Java对象的XML序列化和反序列化 - (1)
- XStream 通过对象流进行对象与xml的序列化和反序列化
- Java对象的XML序列化和反序列化 - (2)
- java 使用xom对象数据序列化为xml、反序列化、Preferences相关操作小案例
- 使用XStream把Java对象序列化为XML
- java 序列化反序列化xml【使用XStream类库】
- 使用XStream对Java对象进行序列化和反序列化
- XStream java对象和json文件进行序列化和反序列化
- Java对象的XML序列化和反序列化 - (3)
- Java 对象的序列化和反序列化
- 对Java 对象 序列化 与 反序列化的 解读