[英]Mapping XML to POJO using xstream
I'm using XStream to map XML to the corresponding POJO. 我正在使用XStream将XML映射到相应的POJO。 My XML structure is as below 我的XML结构如下
<myTag>
<TagABC>
<x> ... </x>
<y> ... </y>
</TagABC>
.
.
.
<TagABC>
<x> ... </x>
<y> ... </y>
</TagABC>
</myTag>
So there are multiple TagABC. 因此有多个TagABC。 I have defined TagABC in my POJO as 我在POJO中将TagABC定义为
private List<TagABCHolder> TagABC;
where TagABCHolder is another POJO that simply contains x, y and their getter, setter 其中TagABCHolder是另一个POJO,仅包含x,y和它们的getter,setter
Now when I try to do the mapping using XStream with the code below 现在,当我尝试使用XStream和以下代码进行映射时
xstream.alias("TagABC", TagABCHolder.class);
xstream.fromXML(xml);
This does not recognize the List structure defined in POJO for TagABC and throws the error below 无法识别POJO中为TagABC定义的List结构,并在下面抛出错误
---- Debugging information ----
message : x: x
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : x: x
class : com.a.b.c.testing.common.TagABCHolder
required-type : java.util.ArrayList
path : /myTag/TagABC/x
line number : 1
Any idea how can I resolve this? 知道我该如何解决吗?
I got the solution. 我找到了解决方案。
This is just a trick with the annotations. 这只是批注的技巧。
Here's what I did: 这是我所做的:
I added @XStreamImplicit annotation in the POJO (TagABCHolder) 我在POJO(TagABCHolder)中添加了@XStreamImplicit批注
@XStreamImplicit(itemFieldName="TagABC")
private List<TagABCHolder> TagABC;
and just processed the annotations placed within the POJO from the code where I was mapping 并刚刚从我映射的代码中处理了放置在POJO中的注释
xstream.processAnnotations(TagABCHolder.class);
That's it!!! 而已!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.