问题描述
我尝试使用简单序列化嵌入式集合。
例如:
I try to serialize embedded collection using simple.For example :
Map<String, List<MyClass>>
我已经在MyClass中添加了必要的注释,我尝试使用@ElementMap,但它不工作:
线程main中的异常org.simpleframework.xml.transform.TransformException:不支持类java.util.ArrayList的转换
I already added necessary annotations in MyClass, i tried with @ElementMap but it doesn't work:Exception in thread "main" org.simpleframework.xml.transform.TransformException: Transform of class java.util.ArrayList not supported
如果只是
@ElementMap Map<String, MyClass>
它工作正常。我不知道要处理嵌入式收藏。我知道 @ElementList
注释,但不知道如何使用它在这种情况下。任何提示?
it works fine. I don't know ho to deal with embedded collection. I know about @ElementList
annotation but don't know how to use it in this case. Any hints?
推荐答案
我遇到同样的问题。
public class MyWrapper {
@ElementList(name="data")
private List<MyClass> data = new ArrayList<MyClass>();
public MyWrapper(List<MyClass> data) {
this.data = data;
}
public List<MyClass> getData() {
return this.data;
}
public void setData(List<MyClass> data) {
this.data = data;
}
}
p>
And then, instead of
@ElementMap Map<String,List<MyClass>>
...您将有:
@ElementMap Map<String,MyWrapper>
在我的例子中,Map对我的类是完全私有的到地图),所以事实上,我有这个额外的层在这里没有太大的区别。生成的XML当然是粗略的,但在我的例子中,它是可以承受的,因为在我的类之外没有什么是消耗它。希望我有一个比这更好的解决方案,但在这一刻,我被困住了。
In my case, the Map is entirely private to my class (i.e. other classes never get to talk directly to the Map), so the fact that I have this extra layer in here doesn't make much of a difference. The XML that is produced of course, is gross, but again, in my case, it's bearable because there is nothing outside of my class that is consuming it. Wish I had a better solution than this, but at the moment, I'm stumped.
这篇关于SimpleXml框架 - 嵌入式集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!