XML和Java对象互转实现-包含List集合对象的情况(XStream方式)
1、XStream引入和使用参考
[参考: XML和Java对象互转实现(XStream方式) - 不无聊 - 博客园 https://www.cnblogs.com/buwuliao/p/15813878.html#/ ]
2、XStream对于List对象的使用
2.1 实体类
2.1.1 MyBeanXStreamXml
package com.MyModel;
import java.util.List;
public class MyBeanXStreamXml {
private String name;
private String id;
private String age;
private List<MyBeanItemXStreamXml> itemList;
//省略get set方法
2.1.2 MyBeanItemXStreamXml
package com.MyModel;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("ROW")
public class MyBeanItemXStreamXml {
private String nameItem;
private String id;
private String age;
//省略get set方法
}
2.2 Demo
注意点: xStreamForStr.autodetectAnnotations(true);
有了这行代码之后,MyBeanItemXStreamXml类中的@XStreamAlias("ROW") 注解才会生效,不然会报错
package com.TestArch;
import com.MyModel.MyBeanItemXStreamXml;
import com.MyModel.MyBeanXStreamXml;
import com.alibaba.fastjson.JSON;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.security.AnyTypePermission;
import lombok.SneakyThrows;
import java.util.ArrayList;
import java.util.List;
public class TestXMLByXStream {
@SneakyThrows
public static void main(String args[]) {
//1、构建对象
List<MyBeanItemXStreamXml> xmls = new ArrayList<>();
MyBeanItemXStreamXml itemXStreamXml= new MyBeanItemXStreamXml();
itemXStreamXml.setNameItem("nameItem");
itemXStreamXml.setId("idItem");
itemXStreamXml.setAge("ageItem");
xmls.add(itemXStreamXml);
MyBeanItemXStreamXml itemXStreamXml2= new MyBeanItemXStreamXml();
itemXStreamXml2.setNameItem("nameItem2");
itemXStreamXml2.setId("idItem2");
itemXStreamXml2.setAge("ageItem2");
xmls.add(itemXStreamXml2);
MyBeanXStreamXml myBeanXml = new MyBeanXStreamXml();
myBeanXml.setAge("28");
myBeanXml.setId("01");
myBeanXml.setName("PHC");
myBeanXml.setItemList(xmls);
// 2、对象转 XML System.out.println("----------对象转 XML-------------------");
XStream xStream = new XStream();
xStream.autodetectAnnotations(true);
String xml = xStream.toXML(myBeanXml);
System.out.println(xml);
// 3、对象转 XML:修改根节点标识
System.out.println("----------对象转 XML:修改根节点标识-------------------");
xStream.alias("ROOT", MyBeanXStreamXml.class); // 修改根节点标识
String xml2 = xStream.toXML(myBeanXml);
System.out.println(xml2);
// 4、XML 字符串转对象
System.out.println("----------XML 字符串转对象-------------------");
XStream xStreamForStr = new XStream();
xStreamForStr.autodetectAnnotations(true);
xStreamForStr.addPermission(AnyTypePermission.ANY); //没有这行会报错:com.thoughtworks.xstream.security.ForbiddenClassException
MyBeanXStreamXml person2 = (MyBeanXStreamXml) xStreamForStr.fromXML(xml);
System.out.println("" + JSON.toJSONString(person2));
}
}
3、附:XStream注解(@XStreamAlias)不生效的报错以及解决:
3.1 报错解决:
原因是:要想使用XStream的注解,必须开启注解扫描。[参考: 要想使用XStream的注解,必须开启注解扫描._boss_way的博客-CSDN博客_xstream 注解没生效 https://blog.csdn.net/boss_way/article/details/78488296#/ ]
解决:xStreamForStr.autodetectAnnotations(true); 添加这行代码即可,具体见Demo
3.2 报错信息
com.thoughtworks.xstream.converters.ConversionException:
---- Debugging information ----
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : ROW
class : java.util.ArrayList
required-type : java.util.ArrayList
converter-type : com.thoughtworks.xstream.converters.collections.CollectionConverter
path : /com.dmall.pay.channel.rpc.dto.vendor.VendorTransactionDetailQueryRpcResponse/LIST/ROW
line number : 1
class[1] : com.dmall.pay.channel.rpc.dto.vendor.VendorTransactionDetailQueryRpcResponse
converter-type[1] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version : 1.4.9
-------------------------------
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:503)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:429)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1230)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1214)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1085)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1076)
at com.dmall.pay.channel.rpc.util.Utils.getObjectFromXML(Utils.java:101)
at com.dmall.pay.channel.rpc.impl.SeparatePayRpcServiceImplTest.getResObj(SeparatePayRpcServiceImplTest.java:447)
at com.dmall.pay.channel.rpc.impl.SeparatePayRpcServiceImplTest.vendorTransactionDetailQuery(SeparatePayRpcServiceImplTest.java:303)
at com.dmall.pay.channel.rpc.impl.SeparatePayRpcServiceImplTest.main(SeparatePayRpcServiceImplTest.java:610)
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: ROW
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:81)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.SecurityMapper.realClass(SecurityMapper.java:71)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:47)
at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:72)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.addCurrentElementToCollection(CollectionConverter.java:98)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:91)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:85)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.unmarshal(CollectionConverter.java:80)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
... 19 more
java.lang.RuntimeException
at com.dmall.pay.channel.rpc.impl.SeparatePayRpcServiceImplTest.getResObj(SeparatePayRpcServiceImplTest.java:451)
at com.dmall.pay.channel.rpc.impl.SeparatePayRpcServiceImplTest.vendorTransactionDetailQuery(SeparatePayRpcServiceImplTest.java:303)
at com.dmall.pay.channel.rpc.impl.SeparatePayRpcServiceImplTest.main(SeparatePayRpcServiceImplTest.java:610)
Exception in thread "main" java.lang.RuntimeException
at com.dmall.pay.channel.rpc.impl.SeparatePayRpcServiceImplTest.vendorTransactionDetailQuery(SeparatePayRpcServiceImplTest.java:310)
at com.dmall.pay.channel.rpc.impl.SeparatePayRpcServiceImplTest.main(SeparatePayRpcServiceImplTest.java:610)