以下是测试的主方法:
package org.ywzn.TestXStream;
import java.util.ArrayList;
import java.util.List;
import org.ywzn.po.Address;
import org.ywzn.po.Phone;
import org.ywzn.po.User;
import org.ywzn.po.XMLTitle;
import com.thoughtworks.xstream.XStream;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
System.out.println("将Java对象转换为xml!\n--------------------------");
List<Address> addresses = new ArrayList<Address>();
addresses.add(new Address("桂林"));
addresses.add(new Address("杭州"));
User user = new User("夏小雪");
Phone phone = new Phone("0010", "魅族MX4 pro", 2299f, user,addresses);
XStream xstream = new XStream();
xstream.alias("Phone", Phone.class);
xstream.alias("User", User.class);
xstream.alias("Addresses", List.class);
xstream.alias("Address", Address.class);
String xml = xstream.toXML(phone);
StringBuilder sendxml = new StringBuilder(XMLTitle.titleInfo);
sendxml.append(xml);
System.out.println(sendxml);
System.out.println("\n将xml转换为Java对象!\n--------------------------");
Object fromXML = xstream.fromXML(sendxml.toString());
if(fromXML instanceof Phone){
System.out.println(fromXML);
}
}
}
输出的结果为:
将Java对象转换为xml!
--------------------------
<?xml version="1.0" encoding="UTF-8"?>
<Phone>
<id>0010</id>
<name>魅族MX4 pro</name>
<price>2299.0</price>
<user>
<name>夏小雪</name>
</user>
<addresses>
<Address>
<city>桂林</city>
</Address>
<Address>
<city>杭州</city>
</Address>
</addresses>
</Phone>
将xml转换为Java对象!
--------------------------
Phone [xmlTitle=null, id=0010, name=魅族MX4 pro, price=2299.0, user=User [name=夏小雪], addresses=[Address [city=桂林], Address [city=杭州]]]
需要的jar包为:
xpp3_min-1.1.4c.jarxstream-1.3.jar
Maven的配置:
<span style="white-space:pre"> </span><dependency>
<groupId>xpp3</groupId>
<artifactId>xpp3_min</artifactId>
<version>1.1.4c</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
</dependency>
package org.ywzn.po;
import java.util.List;
public class Phone {
private XMLTitle xmlTitle;
private String id;
private String name;
private Float price;
private User user;
private List<Address> addresses;
public Phone() {
super();
}
public Phone(String id, String name, Float price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
public Phone(String id, String name, Float price, User user) {
super();
this.id = id;
this.name = name;
this.price = price;
this.user = user;
}
public Phone(String id, String name, Float price, User user,
List<Address> addresses) {
super();
this.id = id;
this.name = name;
this.price = price;
this.user = user;
this.addresses = addresses;
}
public Phone(XMLTitle xmlTitle, String id, String name, Float price,
User user, List<Address> addresses) {
super();
this.xmlTitle = xmlTitle;
this.id = id;
this.name = name;
this.price = price;
this.user = user;
this.addresses = addresses;
}
public XMLTitle getXmlTitle() {
return xmlTitle;
}
public void setXmlTitle(XMLTitle xmlTitle) {
this.xmlTitle = xmlTitle;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
@Override
public String toString() {
return "Phone [xmlTitle=" + xmlTitle + ", id=" + id + ", name=" + name
+ ", price=" + price + ", user=" + user + ", addresses="
+ addresses + "]";
}
}
包含的实体类1:
package org.ywzn.po;
public class Address {
private String city;
public Address() {
super();
}
public Address(String city) {
super();
this.city = city;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address [city=" + city + "]";
}
}
包含的实体类2:
package org.ywzn.po;
public class User {
private String name;
public User() {
super();
}
public User(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [name=" + name + "]";
}
}
xml的标题头信息类:
package org.ywzn.po;
public class XMLTitle {
public static String titleInfo = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
}
Demo案例点击->这里