2017-06-20 94 views
0

如何在解组列表时指定集合实现。对于一些我喜欢使用LinkedList的实体列表,但jaxb为适当的元素创建了ArrayList。列表>是否有最简单的方法来指向集合Impl?Unmarshal list to LinkedList Jaxb

@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); 
    } 
} 
+0

Jaxb应该生成'List >'的形式列表,以便您可以使用任何实现。如果这不是你的情况,你可以添加一些代码示例,其中jaxb生成一个arrayList? –

+0

我已更新文章 –

回答

1

初始化您listProperties作为一个链表:

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

然后解组将返回它作为链接列表。

+0

然后,在创建每个对象时,总是会初始化空列表。你的解决方案有效但也许有一些优雅的解决方案来避免这种行为? –