这是我的问题,我有一个类A
,如下所示:
class A {
public A(MyList myList) {
this.myList = myList;
}
@ElementList(name = "MyList", entry = "MyListElement", type = MyListElement.class)
private MyList myList;
// getters, setters ...
}
我有我的MyList
类,它是一个特殊的ArrayList
:
class MyList extends ArrayList<MyListElement> {
public MyList(Long attribute) {
this.myListAttribute = attribute;
}
@Attribute(name = "MyListAttribute")
private Long myListAttribute;
// getters, setters ...
}
因此,在这里,我的元素列表需要提供一个属性。我发现这就是解决方案(扩展ArrayList<>类),或者可能我错了?
问题是,一切都很好,并且按照我想要的方式序列化,即使是包含属性的MyListElement和许多元素,除了MyList
属性
当我尝试序列化时,我会得到这样的东西:
<A>
<MyList> <!-- Here the attribute is missing... -->
<MyListElement Att1="X" Att2="Something" Att3="Blabla">
<AnElement>Test</AnElement>
<AnotherElement>Test2</AnotherElement>
</MyListElement>
</MyList>
</A>
我想我在文档中遗漏了什么,也许我做错了什么。
提前感谢!
我刚刚找到了另一种方法来做我想做的事。也许我做错了,或者框架没有查看@ElementList
属性上设置的属性。
所以我把我的MyList
类转换成这样:
class MyList { // notice it does not extend ArrayList<MyListElement> anymore
// Now I set this list in inline mode
@ElementList(entry = "MyListElement", type = MyListElement.class, inline = true)
private ArrayList<MyListElement> elementList;
@Attribute(name = "MyListAttribute")
private Long attribute;
}
在我的class A
:中
class A {
@Element(name = "MyList")
private MyList myList;
}
这样,我就得到了我所期望的:
<A>
<MyList MyListAttribute="...">
<MyListElement></MyListElement>
</MyList>
</A>