XStream序列化对象
来源:互联网 发布:ubuntu root 没有声音 编辑:程序博客网 时间:2024/06/06 05:51
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
, xpp3-[version].jar
and xmlpull-[version].jar
in the classpath. Xpp3 is a very fast XML pull-parser implementation. If you do not want to include these dependencies, you can use a standard JAXP DOM parser or since Java 6 the integrated StAX parser instead:
XStream xstream = new XStream(new DomDriver()); // does not require XPP3 library
XStream xstream = new XStream(new StaxDriver()); // does not require XPP3 library starting with Java 6
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序列化对象
- xstream,jackson序列化对象
- XStream 序列化对象到JSON
- XStream序列化对象,java.util.Map自定义Converter
- XStream序列化对象,java.util.Map自定义Converter
- java+xstream实现xml序列化(XStream轻松转换xml和java对象)
- XStream 通过对象流进行对象与xml的序列化和反序列化
- 使用XStream将对象序列化到XML以及从XML反序列化到对象
- 用 XStream 序列化/反序列化 XML 为 Java 对象(实例)
- 用 XStream 序列化/反序列化 XML 为 Java 对象(实例)
- 用 XStream 序列化/反序列化 XML 为 Java 对象(实例)
- XStream java对象和json文件进行序列化和反序列化
- xstream ---java序列化到xml,xml反序列化到java对象(一)
- XStream 序列化与反序列化
- Xstream xml序列化.解析框架
- 使用 XStream 把 Java 对象序列化为 XML
- 使用XStream把Java对象序列化为XML
- 使用 XStream 把 Java 对象序列化为 XML
- 关于BIEE中提示器的使用技巧
- SQL中的left outer join,inner join,right outer join用法详解
- java去空格引出的ASCII十六进制无聊事件
- pomelo iOS/Cocos2d客户端iOS6跑不起来解决办法
- 在已有MySQL情况下安装sphinxSE
- XStream序列化对象
- 中药管理系统软件
- 查看Linux下某个进程里面线程数量
- 解决sqlplus下方向键的问题
- 读取jar包中所有类的信息
- Apache+php+mysql环境搭建
- 解析 简单的字符串计算公式
- gps调试
- 简单看公元2012年的手机市场