XStream fast transform XML
•
Android
Project address: http://xstream.codehaus.org/tutorial.html
(the following is from the official website)
1. Create classes to be serialized
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
}
2. Initializing xStream
XStream xstream = new XStream();
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
xstream.alias("person",Person.class);
xstream.alias("phonenumber",PhoneNumber.class);
3. Serializing an object to XML
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);
<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>
4. Deserializing an object back from XML
Person newJoe = (Person)xstream.fromXML(xml);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
