解组到LinkedList Jaxb的列表

时间:2017-06-20 13:48:54

标签: java jaxb

如何在解组列表时指定集合实现。对于某些实体列表,我更喜欢使用LinkedList,但jaxb为适当的元素生成ArrayList。是否有最简单的方法为List<>指向集合Impl?

    @XmlRootElement(name = "column")
    public class Column {

    @XmlElement(name = "property")
    public List<Property> properties;

    @XmlElement(name = "list-property")
    public List<ListProperty> listProperties;
}

        <column>
            <list-property name="a">...</list-property>
            <list-property name="b">...</list-property>
            <property name="width">10</property>
            <property name="height">20</property>
        </column>

    public Column getObject(File file) {
    try {
        JAXBContext jc = JAXBContext.newInstance(Column.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        return (Column) unmarshaller.unmarshal(file);
    } catch (JAXBException jex) {
        throw new RuntimeException("Object parse was fail", jex);
    }
}

1 个答案:

答案 0 :(得分:1)

将您的listProperties初始化为链接列表:

@XmlElement(name = "list-property")
public List<ListProperty> listProperties = new LinkedList<ListProperty>();

然后unmarshaller将它作为链表返回。