一、XStream
XStream 的github地址:
https://github.com/x-stream/xstream
XStream 的文档地址:
http://x-stream.github.io/index.html
二、基本使用示例
XStreamDemo.java
import java.util.ArrayList;import java.util.List;import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.io.naming.NoNameCoder;import com.thoughtworks.xstream.io.xml.XppDriver;/** * XStream基本使用示例 * * @author Kevin * @version V1.0.0 * @date 2016-4-1 */public class XStreamDemo { public static void main(String[] args) { XStream xstream = new XStream(new XppDriver(new NoNameCoder())); //指定所有class均解析annotations xstream.autodetectAnnotations(true); People people = new People(); people.setId(1L); people.setName("Kevin"); people.setCompanyName("暂时不告诉你"); people.setCity(new City("杭州")); ListproductList = new ArrayList (); productList.add(new ElectronicProduct("MacBook Pro")); productList.add(new ElectronicProduct("iPhone 5S")); productList.add(new ElectronicProduct("iPad Air 2")); people.setElectronicProductList(productList); // Serializing an object to XML String peopleXML = xstream.toXML(people); System.out.println("peopleXML is : \n" + peopleXML); //peopleXML的内容为: /* */ // Deserializing an object back from XML People newpeople = (People) xstream.fromXML(peopleXML); System.out.println("city name is : " + newpeople.getCity().getName()); }} 1 Kevin 暂时不告诉你 杭州 MacBook Pro iPhone 5S iPad Air 2
People.java
import java.util.List;import com.thoughtworks.xstream.annotations.XStreamAlias;/** * 城市里的人 * * @author Kevin * @version V1.0.0 * @date 2016-4-1 */@XStreamAlias("People")public class People { // ID private Long id; // 姓名 private String name; // 所在公司名称 @XStreamAlias("company_name") private String companyName; // 所在城市 @XStreamAlias("City") private City city; // 拥有的电子产品 @XStreamAlias("ElectronicProducts") private ListelectronicProductList; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCompanyName() { return companyName; } public void setCompanyName(String companyName) { this.companyName = companyName; } public City getCity() { return city; } public void setCity(City city) { this.city = city; } public List getElectronicProductList() { return electronicProductList; } public void setElectronicProductList(List electronicProductList) { this.electronicProductList = electronicProductList; }}
City.java
import com.thoughtworks.xstream.annotations.XStreamAlias;/** * 城市类 * * @author Kevin * @version V1.0.0 * @date 2016-4-1 */@XStreamAlias("City")public class City { // 名称 private String name; public City(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
ElectronicProduct.java
import com.thoughtworks.xstream.annotations.XStreamAlias;/** * 电子产品 * * @author Kevin * @version V1.0.0 * @date 2016-4-1 */@XStreamAlias("ElectronicProduct")public class ElectronicProduct { // 名称 private String name; public ElectronicProduct(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
三、使用心得
个人认为XStream不适合用于解析第三方的xml。
第三方xml如果有变动,只要增加一个节点属性,使用XStream解析时就会报错,而且非常难找错误。fastjson的设计就比XStream设计的的要合理,没有的字段不解析出来就可以了,何必导致整个解析报错!
解决方案:
- 重写XStream的ReflectionConverter转换器,忽略没有找到的属性域(推荐使用)。
- 自己再造轮子。