2017-06-20 80 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

然後,在創建每個對象時,總是會初始化空列表。你的解決方案有效但也許有一些優雅的解決方案來避免這種行爲? –