需要的JAR包:
- xstream-1.4.2.jar
- xmlpull-1.1.31.jar
- xpp3_min-1.1.4.jar
示例代码:
package xstream;
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 place;
}
public void setPlace(String place) {
this.place = place;
}
public String toString(){
StringBuffer strBuffer = new StringBuffer();
strBuffer.append("Address").append("\n");
strBuffer.append("addType = ").append(addType).append("\n");
strBuffer.append("place = ").append(place).append("\n");
return strBuffer.toString();
}
}
package xstream;
import java.util.Iterator;
import java.util.List;
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 strBuffer = new StringBuffer();
strBuffer.append("Addresses: ").append("\n");
for(Iterator<Address> it = listAdd.iterator(); it.hasNext();){
Address add = (Address)it.next();
strBuffer.append(add.toString());
}
strBuffer.append("\n");
return strBuffer.toString();
}
}
package xstream;
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 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 Addresses getAddes() {
return addes;
}
public void setAddes(Addresses addes) {
this.addes = addes;
}
public String toString(){
StringBuffer strBuffer = new StringBuffer();
strBuffer.append("Person").append("\n");
strBuffer.append("name = ").append(name).append("\n");
strBuffer.append("sex = ").append(sex).append("\n");
strBuffer.append("tel = ").append(tel).append("\n");
strBuffer.append("addes = ").append(addes.toString()).append("\n");
return strBuffer.toString();
}
}
package xstream;
import java.util.Iterator;
import java.util.List;
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 strBuffer = new StringBuffer();
strBuffer.append("Persons:").append("\n");
strBuffer.append("type = ").append(type).append("\n");
for(Iterator<Person> it = listPerson.iterator(); it.hasNext();){
Person person = (Person)it.next();
strBuffer.append(person.toString());
}
strBuffer.append("\n");
return strBuffer.toString();
}
}
package xstream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import com.thoughtworks.xstream.XStream;
public class XStreamDemo {
public static void main(String[] args) {
testXML2Bean();
}
public static void testXML2Bean(){
System.out.println("将下面的XML对象转换为Bean \n");
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 = readXMLtoObject("d:\\XStream.xml");
Persons persons = (Persons)xstream.fromXML(xml);
System.out.println(persons.toString());
}
public static void testBean2XML(){
System.out.println("将下面的Java对象转换为XML! \n");
XStream xstream = new XStream();
xstream.alias("address", Address.class);
xstream.alias("addresses", Addresses.class);
xstream.alias("person", Person.class);
xstream.alias("persons", Persons.class);
Persons persons = getPersons();
System.out.println(persons.toString());
String xml = xstream.toXML(persons);
writeXML2file(xml, "d:\\XStream.xml");
}
/**
* Structure a person object
* @return
*/
public static Persons getPersons(){
Address add1 = new Address("宿舍", "苏州工业园区菁英公寓");
Address add2 = new Address("公司", "苏州工业园区创意产业园");
List<Address> addList1 = new ArrayList<Address>();
addList1.add(add1);
addList1.add(add2);
Address add3 = new Address("宿舍", "苏州工业园区凤凰城");
Address add4 = new Address("公司", "苏州工业园区万龙智能开关股份有限公司");
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, "Wei", "Man", "123456789");
Person person2 = new Person(addes2, "Juan", "Woman", "987654321");
List<Person> personList = new ArrayList<Person>();
personList.add(person1);
personList.add(person2);
Persons persons = new Persons(personList, "001号家庭");
return persons;
}
/**
* Write XML string into XML file
* @param xml
* @param filePath
*/
public static void writeXML2file(String xml, String filePath){
try {
FileWriter fileWriter = new FileWriter(filePath);
fileWriter.write(xml);
if(fileWriter != null)
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Read XML file into string
* @param filePath
* @return
*/
public static String readXMLtoObject(String filePath){
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
StringBuffer strBuffer = new StringBuffer();
String line = bufferedReader.readLine();
while(line != null){
strBuffer.append(line);
line = bufferedReader.readLine();
}
if(bufferedReader != null)
bufferedReader.close();
return strBuffer.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}