摘自https://www.cnblogs.com/LiZhiW/p/4313493.html
基本使用
<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>
//XStream xstream = new XStream();//需要XPP3库
//XStream xstream = new XStream(new DomDriver());//不需要XPP3库
XStream xstream = new XStream(new StaxDriver());//不需要XPP3库开始使用Java6
//XML序列化
String xml = xstream.toXML(bean);
System.out.println(xml);
//XML反序列化
bean=(Person)xstream.fromXML(xml);
System.out.println(bean);
xstream.alias("人",Person.class);//为类名节点重命名
xstream.aliasPackage("com.lzw", "test");//为包名称重命名
xstream.aliasField("姓名", Person.class,"name");//为类的字段节点重命名
<test.Person>
<姓名>张三</姓名>
<年龄>19</年龄>
</test.Person>
xstream.addImplicitCollection(Person.class, "friends");//省略集合根节点
<test.Person>
<name>张三</name>
<age>19</age>
(+)<string>李四</string>
<string>王五</string>
<string>赵六</string>
</test.Person>
xstream.useAttributeFor(Person.class, "name");//把字段节点设置成属性
<test.Person name="张三">
<age>19</age>
<friends class="java.util.Arrays$ArrayList">
<a class="string-array">
<string>李四</string>
<string>王五</string>
<string>赵六</string>
</a>
</friends>
</test.Person>
Person [name=张三, age=19, friends=[李四, 王五, 赵六]]
xstream.omitField(Person.class, "friends");//把字段节点隐藏
使用注解
XStream xstream = new XStream();
xstream.processAnnotations(Person.class);//应用Person类的注解
xstream.autodetectAnnotations(true);//自动检测注解
重命名:@XStreamAlias()
省略集合根节点:@XStreamImplicit
把字段节点设置成属性:@XStreamAsAttribute
隐藏字段:@XStreamOmitField
设置转换器:@XStreamConverter()
其他用法
## 流
XStream xstream = new XStream();
ObjectOutputStream out = xstream.createObjectOutputStream(System.out);
out.writeObject(new Person("张三",12));
out.writeObject(new Person("李四",19));
out.writeObject("Hello");
out.writeInt(12345);
out.close();
String s="<object-stream><test.Person><name>张三</name><age>12</age></test.Person><int>12345</int></object-stream>";
StringReader reader = new StringReader(s);
XStream xstream = new XStream();
ObjectInputStream in = xstream.createObjectInputStream(reader);
System.out.println((Person) in.readObject());
System.out.println(in.readInt());
## 持久化
PersistenceStrategy strategy = new FilePersistenceStrategy(new File("D:\\tmp"));
List list = new XmlArrayList(strategy);
list.add(new Person("张三",13));//保存数据
list.add(new Person("李四",21));
list.add(new Person("王五",17));
PersistenceStrategy strategy = new FilePersistenceStrategy(new File("D:\\tmp"));
List list = new XmlArrayList(strategy);
for (Iterator it = list.iterator(); it.hasNext();)
{
System.out.println((Person) it.next());
it.remove();//删除对象序列化文件
}
自封装类
public class XmlHelper {
@Data
@XStreamAlias("XMLInfo")
public class PrintSyncDto {
private String merchantCode;
private String serviceOrderNo;
private String client;
private String sercet;
private String nonceStr;
private String signature;
private PrintData printData;
@Data
public static class PrintData {
private String useSenderInfo;
private String bigAccountDataId;
private String businessType;
}
}
public static void main(String[] args) {
XStream xstream = getXStream();
PrintSyncDto printSyncDto = new PrintSyncDto();
printSyncDto.setMerchantCode("1");
printSyncDto.setServiceOrderNo("2");
printSyncDto.setClient("3");
printSyncDto.setSercet("4");
printSyncDto.setNonceStr("5");
printSyncDto.setSignature("6");
PrintSyncDto.PrintData printData = new PrintSyncDto.PrintData();
printData.setUseSenderInfo("7");
printData.setBigAccountDataId("8");
printData.setBusinessType("9");
printSyncDto.setPrintData(printData);
String xml = xstream.toXML(printSyncDto);
System.out.println(xml);
String json = "";
System.out.println(json = XmlHelper.xmlToJson(xml, xstream));
System.out.println((XmlHelper.jsonStrToXml(json, PrintSyncDto.class, xstream)));
// System.out.println(JSON.toJSONString(xstream.fromXML(xml)));
}
public static final XStream getXStream() {
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);//自动检测注解
return xstream;
}
public static final String xmlToJson(String xml, XStream xstream) {
Object o = xstream.fromXML(xml);
return JSON.toJSONString(o);
}
public static final <T> String jsonToXml(JSONObject jo, Class<T> clazz, XStream xstream) {
return xstream.toXML(JSON.toJavaObject(jo, clazz));
}
public static final <T> String jsonStrToXml(String json, Class<T> clazz, XStream xstream) {
return xstream.toXML(JSON.parseObject(json, clazz));
}
}
网友评论