您的位置:首页 > 编程语言 > Java开发

XStream基本使用

2016-04-01 00:00 483 查看
摘要: 记录XStream的基本使用方法。

一、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("杭州"));

List<ElectronicProduct> productList = new ArrayList<ElectronicProduct>();
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的内容为:
/*
<People>
<id>1</id>
<name>Kevin</name>
<company_name>暂时不告诉你</company_name>
<City>
<name>杭州</name>
</City>
<ElectronicProducts>
<ElectronicProduct>
<name>MacBook Pro</name>
</ElectronicProduct>
<ElectronicProduct>
<name>iPhone 5S</name>
</ElectronicProduct>
<ElectronicProduct>
<name>iPad Air 2</name>
</ElectronicProduct>
</ElectronicProducts>
</People>
*/

// Deserializing an object back from XML
People newpeople = (People) xstream.fromXML(peopleXML);
System.out.println("city name is : " + newpeople.getCity().getName());
}
}

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 List<ElectronicProduct> electronicProductList;

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<ElectronicProduct> getElectronicProductList() {
return electronicProductList;
}

public void setElectronicProductList(List<ElectronicProduct> 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转换器,忽略没有找到的属性域(推荐使用)。

自己再造轮子。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  XStream java xml