2016-04-25 43 views
0

嗨我正在使用JAXB(JAVA)編組XML。如何在JAXB XML編組中顯示空列表?

我有一些有時大小爲零的XmlList元素。

由於它構造實際的數組列表時吸氣劑被調用時,

輸出總是顯示空元素,如

<aa></aa> 

反正是有消除這些「空」元素?

謝謝。

+0

我猜 - 讓您的收藏空的,而不是空。 – john

+0

像單身人士的懶惰初始化。第一次插入時,讓你的類負責創建一個'List'的實例,一旦創建就使用相同的。同樣,從列表中刪除也應該謹慎。 – soufrk

+0

出於某種原因,我生成的源getter具有如下 公開列表 getPointOfAccess(){ if(pointOfAccess == null){ pointOfAccess = new ArrayList (); } return this.pointOfAccess; } –

回答

0
public void representingNullAndEmptyCollections() throws Exception { 
    JAXBContext jc = JAXBContext.newInstance(Root.class); 

    Root root = new Root(); 

    root.nullCollection = null; 

    root.emptyCollection = new ArrayList<String>();// Shows a Empty Element 

    root.populatedCollection = new ArrayList<String>(); 
    root.populatedCollection.add("foo"); 
    root.populatedCollection.add("bar"); 

    Marshaller marshaller = jc.createMarshaller(); 
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
    marshaller.marshal(root, System.out); 
} 
0

一般null字段不是由JAXB呈現。訣竅是使用一個特殊的getter。

人們普遍簡單:

public List<String> getStuff() { 
    return stuff.isEmpty() ? null : stuff; 
}