Using XStream to convert between Java objects and XML

Source: Internet
Author: User
Tags stringbuffer
using XStream to convert between Java objects and XMLXStream is a good thing, easy to convert between Java objects and XML, efficiency and readability are very high, the following is a test example I wrote, let's take a look. The required jar packages are as follows: Xpp3_min-1.1.4c.jar
Xstream-1.3.jar Goal: Convert the persons object to XML, and then convert the XML into a persons object. implementation Source list: Package Com.lavasoft.demo3;

Import Com.thoughtworks.xstream.XStream;

Import java.util.List;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import Java.io.StringReader;
Import Java.io.FileReader;
Import Java.io.File;
Import java.io.FileNotFoundException;


/**
* Created by IntelliJ idea.
* File:demo3.java
* User:leizhimin
* Date:2008-3-4 14:44:03
*/
public class Xstreamdemo {
public static void Main (String args[]) {
Testbean2xml ();
}

/**
* Generate a Persons object
*
* @return Persons objects
*/
public static Persons Getpersons () {
Address ADD1 = new Address ("Type1", "Zhengzhou Fortune Plaza 1");
Address ADD2 = new Address ("Type2", "Zhengzhou Fortune Plaza 2");
list<address> addlist1 = new arraylist<address> ();
Addlist1.add (ADD1);
Addlist1.add (ADD2);

Address ADD3 = new Address ("Type3", "Zhengzhou Fortune Plaza 3");
Address ADD4 = new Address ("Type4", "Zhengzhou Fortune Plaza 4");
list<address> addlist2 = new arraylist<address> ();
Addlist2.add (ADD3);
Addlist2.add (ADD4);

Addresses addes1 = new Addresses (ADDLIST1);
Addresses addes2 = new Addresses (ADDLIST2);
Person Person1 = new Person (addes1, "6666554", "Lavasoft", "Mans");
Person Person2 = new Person (addes2, "7777754", "Yutian", "Mans");

list<person> Listperson = new arraylist<person> ();
Listperson.add (Person1);
Listperson.add (Person2);
Persons Persons = new Persons (Listperson, "001");
return persons;
}

/**
* Use XStream to convert between Java objects and XML
*/
public static void Testbean2xml () {
SYSTEM.OUT.PRINTLN ("Converts Java objects to XML.) \ n ");
Persons Persons = Getpersons ();
XStream XStream = new XStream ();
Xstream.alias ("Address", address. Class);
Xstream.alias ("Addresses", addresses. Class);
Xstream.alias ("person", person. Class);
Xstream.alias ("Persons", persons. Class);
String XML = xstream.toxml (persons);
SYSTEM.OUT.PRINTLN (XML);

System.out.println ("\ n) converts the XML to a Java object. ");
Persons Cre_person = (Persons) xstream.fromxml (XML);
System.out.println (Cre_person.tostring ());
}
} public class Persons {
Private String type;
Private list<person> Listperson;

Public Persons (list<person> Listperson, String type) {
This.listperson = Listperson;
This.type = type;
}

Public String GetType () {
return type;
}

public void SetType (String type) {
This.type = type;
}

Public list<person> Getlistperson () {
return Listperson;
}

public void Setlistperson (list<person> listperson) {
This.listperson = Listperson;
}

Public String toString () {
StringBuffer sb= new StringBuffer ();
For (iterator It=listperson.iterator (); It.hasnext ();) {
Person p= (person) it.next ();
Sb.append (It.tostring ());
}
Return "persons{" +
"Type= '" + type + ' \ ' +
", listperson=" + sb.tostring () +
"}\n";
}
} public class Person {
private String name;
Private String sex;
Private String Tel;
Private Addresses addes;

Public person (Addresses addes, string name, String sex, String tel) {
This.addes = Addes;
THIS.name = name;
This.sex = sex;
This.tel = Tel;
}

Public Addresses Getaddes () {
return addes;
}

public void Setaddes (Addresses addes) {
This.addes = Addes;
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

Public String Getsex () {
return sex;
}

public void Setsex (String sex) {
This.sex = sex;
}

Public String Gettel () {
return Tel;
}

public void Settel (String tel) {
This.tel = Tel;
}

Public String toString () {
Return "person{" +
"Addes=" + addes.tostring () +
", Name= '" + name + ' \ ' +
", sex= '" + sex + ' + "+
", tel= '" + Tel + ' + "+
"}\n";
}
} public class Addresses {
Private list<address> Listadd;

Public Addresses (list<address> listadd) {
This.listadd = Listadd;
}

Public list<address> Getlistadd () {
return listadd;
}

public void Setlistadd (list<address> listadd) {
This.listadd = Listadd;
}

Public String toString () {
StringBuffer sb= new StringBuffer ();
For (iterator It=listadd.iterator (); It.hasnext ();) {
Address add= (address) it.next ();
Sb.append (Add.tostring ());
}
Return "addresses{" +
"Listadd=" + sb.tostring () +
"}\n";
}
} public class Address {
Private String AddType;
Private String Place;

Public address (string AddType, String place) {
This.addtype = AddType;
This.place = place;
}

Public String Getaddtype () {
return AddType;
}

public void Setaddtype (String addtype) {
This.addtype = AddType;
}

Public String Getplace () {
return to place;
}

public void Setplace (String place) {
This.place = place;
}


Public String toString () {
Return "address{" +
"Addtype= '" + addtype + ' + ' +
", place= '" + place + ' \ ' +
"}\n";
}
Run the test class Xstreamdemo and print out the result: Convert Java objects to XML.

< persons >
< type >001 </type >
< Listperson >
< person >
< name >6666554 </name >
< sex >lavasoft </sex >
< Tel >man </Tel >
< Addes >
< Listadd >
< address >
< AddType >type1 </AddType >
< place > Zhengzhou Fortune Plaza 1 </Square >
</address >
< address >
< AddType >type2 </AddType >
< place > Zhengzhou Fortune Plaza 2 </Square >
</address >
</listadd >
</addes >
</person >
< person >
< name >7777754 </name >
< sex >yutian </sex >
< Tel >man </Tel >
< Addes >
< Listadd >
< address >
< AddType >type3 </AddType >
< place > Zhengzhou Fortune Plaza 3 </Square >
</address >
< address
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.