我们在工作中经常 遇到文件解析为数据或者数据转化为xml文件的情况,之前我一直采用的方式都是手动的来解析或拼接XML文件,这个做法固然是可以的,但是代码部分会显得非常臃肿,所以就查找了相关的资料,发现了一个名为XStream的类库可以做这件事,下面我们来看一下.
官网地址:http://x-stream.github.io/
从官网介绍我们能看到以下几个特性:
从XStream的官网我们了解到,XStream不需要Mapping文件就能完成JavaBean和XML之间的转变,但大部分时候我们需要对生成的XML文件进行定制,name我们需要了解XStream提供的注解
注解 | 作用 | 适用范围 |
---|---|---|
@XStreamAlias | 设置别名 | 类或属性 |
@XStreamImplicit | 忽略属性 | 集合类型的属性 |
@XStreamAsAttribute | 将JavaBean转换成节点属性 | 属性 |
@XStreamOmitField | 忽略属性 | 属性 |
@XStreamConverter | 注入转换器 | 类 |
首先在我们的项目中加入依赖:
com.thoughtworks.xstream
xstream
1.4.11.1
假定环境为Computer和CPU两个类:
public class Computer implements Serializable {
private static final long serialVersionUID = 567119860357020081L;
private String name;
private String brand;
private List processors;
public Computer() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public List getProcessors() {
return processors;
}
public void setProcessors(List processors) {
this.processors = processors;
}
}
public class CPU implements Serializable {
private static final long serialVersionUID = -718438777162957779L;
private String name;
private String generation;
public CPU() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGeneration() {
return generation;
}
public void setGeneration(String generation) {
this.generation = generation;
}
}
写一个测试方法:
import com.hykj.facheck.entity.CPU;
import com.hykj.facheck.entity.Computer;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.util.ArrayList;
import java.util.List;
public class XStreamTest {
public static void main(String[] args) {
List cpuList = new ArrayList<>();
CPU cpu1 = new CPU();
cpu1.setGeneration("8-i5-K");
cpu1.setName("第一个");
cpuList.add(cpu1);
CPU cpu2 = new CPU();
cpu2.setGeneration("8-i5-U");
cpu2.setName("第二个");
cpuList.add(cpu2);
Computer computer = new Computer();
computer.setBrand("huawei");
computer.setName("wej-laptop");
computer.setProcessors(cpuList);
XStream xstream = new XStream(new DomDriver());
xstream.ignoreUnknownElements();
xstream.processAnnotations(Computer.class);
//javabean to xml
String xml = xstream.toXML(computer);
System.out.println(xml);
//xml to javabean
Computer computerFromXml = (Computer) xstream.fromXML(xml);
System.out.println(computer.getName());
}
}
运行程序,控制台打结果如下:
Connected to the target VM, address: '127.0.0.1:53977', transport: 'socket'
wej-laptop
huawei
第一个
8-i5-K
第二个
8-i5-U
Security framework of XStream not initialized, XStream is probably vulnerable.
wej-laptop
Disconnected from the target VM, address: '127.0.0.1:53977', transport: 'socket'
Process finished with exit code 0
也就是说这样就可以用了,很棒棒
但是这个XML和我们的需求不太一样,我们加上注解,代码变成了这样:
import com.thoughtworks.xstream.annotations.XStreamAlias;
import java.io.Serializable;
import java.util.List;
@XStreamAlias("my")
public class Computer implements Serializable {
private static final long serialVersionUID = 567119860357020081L;
private String name;
private String brand;
@XStreamAlias("cpuList")
private List processors;
public Computer() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public List getProcessors() {
return processors;
}
public void setProcessors(List processors) {
this.processors = processors;
}
}
import com.thoughtworks.xstream.annotations.XStreamAlias;
import java.io.Serializable;
@XStreamAlias("oneCpu")
public class CPU implements Serializable {
private static final long serialVersionUID = -718438777162957779L;
private String name;
private String generation;
public CPU() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGeneration() {
return generation;
}
public void setGeneration(String generation) {
this.generation = generation;
}
}
测试方法不变,结果如下:
Connected to the target VM, address: '127.0.0.1:54181', transport: 'socket'
wej-laptop
huawei
第一个
8-i5-K
第二个
8-i5-U
Security framework of XStream not initialized, XStream is probably vulnerable.
wej-laptop
Disconnected from the target VM, address: '127.0.0.1:54181', transport: 'socket'
Process finished with exit code 0
这个XML就很舒服啦.
我们载试一下循环引用的情况吧,代码如下:
import com.thoughtworks.xstream.annotations.XStreamAlias;
import java.io.Serializable;
import java.util.List;
@XStreamAlias("my")
public class Computer implements Serializable {
private static final long serialVersionUID = 567119860357020081L;
private String name;
private String brand;
@XStreamAlias("cpuList")
private List processors;
public Computer() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public List getProcessors() {
return processors;
}
public void setProcessors(List processors) {
this.processors = processors;
}
}
import com.thoughtworks.xstream.annotations.XStreamAlias;
import java.io.Serializable;
@XStreamAlias("oneCpu")
public class CPU implements Serializable {
private static final long serialVersionUID = -718438777162957779L;
private String name;
private String generation;
private Computer computer;
public CPU() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGeneration() {
return generation;
}
public void setGeneration(String generation) {
this.generation = generation;
}
public Computer getComputer() {
return computer;
}
public void setComputer(Computer computer) {
this.computer = computer;
}
}
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.util.ArrayList;
import java.util.List;
public class XStreamTest {
public static void main(String[] args) {
Computer computer = new Computer();
computer.setBrand("huawei");
computer.setName("wej-laptop");
List cpuList = new ArrayList<>();
CPU cpu1 = new CPU();
cpu1.setGeneration("8-i5-K");
cpu1.setName("第一个");
cpu1.setComputer(computer);
cpuList.add(cpu1);
CPU cpu2 = new CPU();
cpu2.setGeneration("8-i5-U");
cpu2.setName("第二个");
cpuList.add(cpu2);
cpu1.setComputer(computer);
computer.setProcessors(cpuList);
XStream xstream = new XStream(new DomDriver());
xstream.ignoreUnknownElements();
xstream.processAnnotations(Computer.class);
//javabean to xml
String xml = xstream.toXML(computer);
System.out.println(xml);
//xml to javabean
Computer computerFromXml = (Computer) xstream.fromXML(xml);
System.out.println(computer.getName());
}
}
运行一下,先查看是否构成循环引用:
确认有循环引用,继续运行代码,控制台打印如下:
Connected to the target VM, address: '127.0.0.1:54221', transport: 'socket'
wej-laptop
huawei
第一个
8-i5-K
第二个
8-i5-U
Security framework of XStream not initialized, XStream is probably vulnerable.
wej-laptop
Disconnected from the target VM, address: '127.0.0.1:54221', transport: 'socket'
Process finished with exit code 0
可以看到生成的XML中采用类似路径的reference属性来标识了位置,实际上我们一般是需要忽略这个属性的,采用@XStreamOmitField注解就好了.