First go to the official website to download the response jar package
Http://xstream.codehaus.org/index.html
A minimum of two jar packages, one xstream.jar and one Xmlpull.jar
First look at the translation of the Java object to XML
Package Xml;class PhoneNumber {private int code;private int number;public int GetCode () {return code;} public void Setcode (int code) {this.code = code;} public int GetNumber () {return number;} public void Setnumber (int number) {this.number = number;}} public class Person {private String username;private int age;private phonenumber pn;public String getusername () {return US Ername;} public void Setusername (String username) {this.username = username;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public PhoneNumber GETPN () {return PN;} public void SETPN (PhoneNumber pn) {this.pn = pn;}}
The above is an ordinary Java object
Package Xml;import Com.thoughtworks.xstream.xstream;import Com.thoughtworks.xstream.io.xml.staxdriver;public class xmlutils {private static XStream xs = new XStream (new Staxdriver ());p ublic static String objtoxml (Object obj) {//xs.aliasp Ackage (name, pkgname); return xs.toxml (obj);} public static void Main (string[] args) {person p = new person ();p. Setage (;p. Setusername ("Chiwei"); PhoneNumber pn = new PhoneNumber ();p n.setcode (188);p N.setnumber (67101080);p. SETPN (PN); Xs.alias ("Person", Person.class); String xml = Objtoxml (p); Person per = (person) xs.fromxml (XML); SYSTEM.OUT.PRINTLN (XML); System.out.println (Per.getusername ());}}
The above code first generates an object, assigns values to the individual properties, and then calls ToXml to convert the object to plain text in XML format, noting that the alias was called before the conversion to make the XML. person renamed to Person
<?xml version= "1.0"? ><person><username>chiwei</username><age>26</age><pn ><code>188</code><number>67101080</number></pn></person>
Here XStream instantiates the staxdriver representation of a driver using the StAX API to create XML reader and writer.
If you use Xpp3driver, you need to introduce the JAR package
Later, the converted XML continues to be converted to the corresponding object.
<?xml version= "1.0"? ><person><username>chiwei</username><age>26</age><pn ><code>188</code><number>67101080</number></pn></person>
Chiwei
Very simple
XStream implements the conversion between XML and Java objects, and the same JSON can