2012-09-18 3 views
6

JAXB를 사용하면 null 값이 빈 요소로 정렬되지 않도록 할 수 있습니다. 전화 요소 중 하나가 null의 경우, 예를 들어jaxb 마샬링 빈 요소 건너 뛰기

public class Contacts { 
    @XmlElement(name = "Phone") 
    protected List<Phone> phone; 
    } 

현재 내가

<contact> 
     </phone> 
     <phone> 
       <areacode>919</areacode> 
       <phonenumber>6785432</phonenumber> 
     </phone> 
    </contact> 

내가

<contact> 
      <phone> 
        <areacode>919</areacode> 
        <phonenumber>6785432</phonenumber> 
      </phone> 
    </contact> 

답변

4

널 값이 빈 요소로 마샬링하지 않은 다음 출력을 할 수 기본적으로입니다.
비어있는 값만 비어있는 요소로 정렬됩니다.

예에서 비어있는 Phone object 요소가있는 컬렉션을 사용하고 있습니다. 목록에 두 개의 요소, 즉 empty Phone (모든 필드는 null) 및 null 필드가없는 Phone object이 있습니다.

public class Contacts { 
    @XmlElement(name = "Phone") 
    protected List<Phone> phone = Arrays.asList(new Phone[]{null, null, null}); 
} 

<contact/> 

하지만

public class Contacts { 
    @XmlElement(name = "Phone") 
    protected List<Phone> phone = Arrays.asList(new Phone[]{new Phone(), new Phone(), null}); 
} 

에 정렬 화됩니다
따라서는,

<contact> 
    <Phone/> 
    <Phone/> 
</contact> 
에 정렬 화합니다