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;
/**
* 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 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
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.