The XStream object is quite a translator between Java objects and XML, and the conversion process is bidirectional. The way to create a Xsteam object is simple, you just need a new XStream ().
Java to XML, using the ToXml () method.
XML to Java, using the FromXml () method.
In the absence of any default configuration, the Java to XML mapping is the Java member name corresponding to the XML element name, the Java class's full name corresponds to the XML root element's name. In practice, there are often XML and Java classes, and to complete the conversion, alias mapping must be done.
The alias configuration contains three different scenarios:
1, category name, with Alias (Stringname, class type).
2, class member Alias, with Aliasfield (Stringalias, Class Definedin, String fieldName)
3, class members as property aliases, with Aliasattribute (class Definedin, String AttributeName, string alias), separate naming meaningless, but also through the useattributefor ( Classdefinedin, String fieldName) is applied to a class.
Xstreamannotation
@XStreamAlias ("Alias Name"): Usedto Alias Class Name and field
@XStreamAsAttribute (): Add attribute for Class
@XStreamImplicit () or @XStreamImplicit (Itemfieldname = "Group") to reassign the name
XML Parser
1. XPP3 (need Xpp3-[version].jar)
XStream XStream = new XStream (new Xppdriver ());
2. JAXP DOM
XStream XStream = new XStream (new Domdriver ()); Do not specify the encoding parser
XStream XStream = new XStream (Newdomdriver ("UTF-8")); Specify the Encoding parser
Some small bugs in xstreamd
--After the underscore "_" in the definition alias is converted to XML, it becomes the symbol "__". Therefore, try to avoid any symbol in the alias, but when you need to underline, you can consider the practical connector "-", this is not a problem.
--java beans often have constants in the conversion process, XStream will also convert these constants to form a constant XML node, which is obviously not the desired result, for constant fields, do not convert.
Code Listing 1:
Package test;
Importcom.thoughtworks.xstream.annotations.XStreamAlias;
Importcom.thoughtworks.xstream.annotations.XStreamAsAttribute;
@XStreamAlias ("Address")
public class Address {
Private String add;
@XStreamAsAttribute ()
Private String ZipCode;
Public address (string add, String zipcode) {
This.add = add;
This.zipcode = ZipCode;
}
Public String toString () {
Return "address{"
+ "add= '" + add + ' \ '
+ ", zipcode= '" +zipcode + ' "
+ '}';
}
}
Code Listing 2:
Package test;
Importcom.thoughtworks.xstream.annotations.XStreamAlias;
Importcom.thoughtworks.xstream.annotations.XStreamOmitField;
Import java.util.List;
@XStreamAlias ("person")
public class Person {
@XStreamAlias ("Name")
private String name;
Use @xstreamomitfield to annotate fields that do not generate XML
The code then uses Xstream.autodetectannotations (true); You can finish removing the attributes.
@XStreamOmitField
Private String age;
Private Profileprofile;
@XStreamImplicit ()
Private list<address> addlist;
Public person (string name, String age, Profile profile,list<address> addlist) {
THIS.name = name;
This.age = age;
This.profile = profile;
This.addlist = addlist;
}
Public String toString () {
Return "person{"
+ "Name= '" + name + ' \ '
+ ", age= '" + Age + ' \ "
+ ", profile=" +profile
+ ", addlist=" +addlist
+ '}';
}
}
Code Listing 3:
Package test;
Importcom.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias ("Profile")
public class Profile {
Private String job;
Private String Tel;
Private String remark;
Public Profile (String job, String tel, string remark) {
This.job = job;
This.tel = Tel;
This.remark = remark;
}
Public String toString () {
Return "profile{"
+ "job= '" + job + ' \ '
+ ", tel= '" + Tel + ' \ "
+ ", remark= '" +remark + ' "
+ '}';
}
}
Code Listing 4:
/*
* Tochange This template, choose Tools | Templates
*and Open the template in the editor.
*/