XStream implements the conversion between XML and Java objects, and the same JSON can

Source: Internet
Author: User
Tags xml reader

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.