开发者

How to marshall ArrayList<Object>[] in JAXB?

开发者 https://www.devze.com 2023-03-28 07:48 出处:网络
I have a vari开发者_如何学编程able (list) of type ArrayList[] and I want to save it in XML. I tried JAXB, but it saves only the \"\" String (the repetition of \" is equal to list.length) and no items

I have a vari开发者_如何学编程able (list) of type ArrayList[] and I want to save it in XML. I tried JAXB, but it saves only the "" String (the repetition of " is equal to list.length) and no items in ArrayLists. If I tried the 2d array it works fine. If I tried ArrayList, it works also fine. How can I solve this problem?

My code is similar to:

@XmlRootElement
public class SomeClass {

    @XmlElement(name="part")
    private final ArrayList<Object>[] list;

    ... constructor, which fills the list variable

}

Can someone tell me how to do this? Please.


You should not mix generic types with arrays - Look at Item 25 in Effective Java. Use 2D array or list of lists.


Thank yout for your effort, but I finally found a solution :). I covered one list by another class and it's working fine.:

@XmlElement(name="part)
MyClass[] list;

@XmlRootElement
class MyClass {
    @XmlElement(name="item")
    ArrayList<Object> list;    
}


Do you really need an array of list objects? If not you could also use two lists:

Private LinkedList<LinkedList<Object>> parts;

This works well with jaxb.

0

精彩评论

暂无评论...
验证码 换一张
取 消