一、Xstream引入
1、背景说明
Xstream是一个功能比较强大的xml和java对象互转的工具包。可以通过简单的api调用,便实现一系列的转换操作。大家都知道微信的接口调用都是通过xml的形式进行数据交互的,所以在一些传统的接口调用和微信开发过程当中尤其适用。
2、项目依赖
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>
二、使用说明
使用继承共用类属性时,@XStreamAlias别名注解要使用在子类上来标记节点。否则会出现反射出错的问题。
@XStreamAsAttribute注解可以使定义对象的属性映射为xml文件的属性。
@XStreamImplicit注解来标记不需要在xml文件中出现的节点信息
Xstream提供了好多种driver来方便进行转换时选择使用。如果使用了xpp3相关的驱动,则需要另外引入xpp3相关的依赖。
具体使用示例如下(普通编码实现):
XStream xstream = new XStream();
xstream.alias("person", Person.class);
Person joe = new Person("Joe", "Walnes");
String xml = xstream.toXML(joe);
这样,输出的xml字串即为对象转换出的xml内容。另外,还可以使用Xstream提供的toXML的其他方法,进行转换内容输出到文件。具体为:
XStream xStream = new XStream();
xstream.alias("person", Person.class);
Person obj = new Person("Joe", "Walnes");
xStream.toXML(obj,new FileOutputStream(new File("xxx.xml")));
使用注解实现 使用注解实现转换时,除了要知道上面介绍的几个主要注释之外,还要在初始化玩Xstream对象之后,进行处理注解的设置,如下:
XStream xStream = new XStream();
xStream.processAnnotations(XXX.class);
// XXX为具体的根节点类,可以使用@XStreamAlias标注别名,也可以不用。
自己封装的工具类
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.DateConverter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.TimeZone;
/**
* @author mark
* @version 1.0
* @description xml与Bean互转工具类
* @since 2018/7/12
*/
public class XmlParseUtil {
private static final DomDriver DOM_DRIVER =
new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_"));
private static final DateConverter DATE_CONVERTER =
new DateConverter("yyyy-MM-dd HH:mm:ss", null, TimeZone.getTimeZone("GMT+8"));
/**
* @param filePath file's path
* @param clz the class which is target Bean
* @param <T>
* @return the bean parse from xml
*/
public static <T> T xml2Bean(String filePath, Class<T> clz) {
// loading driver
// xstream也业提供了json的转换驱动,JettisonMappedXmlDriver和JsonHierarchicalStreamDriver。
XStream xStream = new XStream(DOM_DRIVER);
// setup security configuration
// setupDefaultSecurity to be removed after 1.5
XStream.setupDefaultSecurity(xStream);
xStream.allowTypesByWildcard(new String[] {"com.shuzhi.**"});
// register converter
xStream.registerConverter(DATE_CONVERTER);
// processing annotations
xStream.processAnnotations(clz);
// processing xml
T t = (T) xStream.fromXML(new File(filePath));
return t;
}
/**
* return string of obj parsed by xstream.
*
* @param obj
* @return
*/
public static String toXml(Object obj) {
XStream xStream = new XStream(DOM_DRIVER);
// register converter
xStream.registerConverter(DATE_CONVERTER);
// processing annotations
xStream.processAnnotations(obj.getClass());
return xStream.toXML(obj);
}
/**
* parse obj to xml and write to file.
*
* @param obj
* @param filePath file path
*/
public void outputFile(Object obj, String filePath) throws FileNotFoundException {
XStream xStream = new XStream(DOM_DRIVER);
// register converter
xStream.registerConverter(DATE_CONVERTER);
// processing annotations
xStream.processAnnotations(obj.getClass());
// create target file
FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath));
// output
xStream.toXML(obj, fileOutputStream);
}
}
来源:CSDN
作者:Aries、zn
链接:https://blog.csdn.net/abczhaonan123/article/details/104152487