2013-05-29 60 views
1

下列要求:JAXB對象不會元帥/解組列表屬性

@XmlRootElement(name = "purchase") 
@XmlType(propOrder = {"memberId", "propertyA", "propertyB", "propertyC", "listProps"}) 
public class ClassA { 

    private Long memberId; 
    private Integer propertyA; 
    private String propertyB; 
    private Integer propertyC; 
    private List<ClassB> listProps; 

    public ClassA() { 
    } 

    @XmlElement(name = "memberId") 
    public Long getMemberId() { 
     return memberId; 
    } 

    public void setMemberId(Long memberId) { 
     this.memberId = memberId; 
    } 

    @XmlElement(name = "propertyA") 
    public Integer getPropertyA() { 
     return propertyA; 
    } 

    public void setPropertyA(Integer propertyA) { 
     this.propertyA = propertyA; 
    } 

    @XmlElement(name = "propertyB") 
    public String getPropertyB() { 
     return propertyB; 
    } 

    public void setPropertyB(String propertyB) { 
     this.propertyB = propertyB; 
    } 

    @XmlElement(name = "propertyC") 
    public Integer getPropertyC() { 
     return propertyC; 
    } 

    public void setPropertyC(Integer propertyC) { 
     this.propertyC = propertyC; 
    } 

    @XmlElement(name = "listProps") 
    public List<ClassB> getListProps() { 
     return listProps; 
    } 

    public void setListProps(List<ClassB> listProps) { 
     this.listProps = listProps; 
    } 
} 
@XmlRootElement(name = "listProp") 
@XmlType(propOrder = {"countA", "countB"}) 
public class ClassB { 

    private int countA; 
    private int countB; 

    public ClassB() { 
    } 

    public int getCountA() { 
     return countA; 
    } 

    public int getCountB() { 
     return countB; 
    } 

    @XmlElement(name = "countA") 
    public void setCountA(int countA) { 
     this.countA = countA; 
    } 

    @XmlElement(name = "countB") 
    public void setCountB(int countB) { 
     this.countB = countB; 
    } 
} 

當我嘗試和元帥的ClassA類型的/解組對象的listProps不管是多少物體總是空的我已經投入了它。誰能告訴我我做錯了什麼?

回答

0

當我封你的模型類,如下所示:

import java.util.*; 
import javax.xml.bind.*; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(ClassA.class); 


     List<ClassB> classBs = new ArrayList<ClassB>(); 
     classBs.add(new ClassB()); 
     classBs.add(new ClassB()); 

     ClassA classA = new ClassA(); 
     classA.setListProps(classBs); 

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

} 

我得到下面的輸出,所以與你的列表屬性沒有問題:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<purchase> 
    <listProps> 
     <countA>0</countA> 
     <countB>0</countB> 
    </listProps> 
    <listProps> 
     <countA>0</countA> 
     <countB>0</countB> 
    </listProps> 
</purchase> 
0

據我瞭解你的問題是取消編組您已編組的值列表。當解析結果到空列表中時,我遇到了與jaxb-impl lib + 2.2.x相同的問題,而XML至少包含1個元素。如果getListProps方法中的值爲null,嘗試實例化列表,以便JAXB可以填充它。我覺得問題出在List + XmlAccessorType.PROPERTY中,因爲它默認不會創建列表並嘗試使用現有的列表,因爲它是null setListProps是用空集合調用的。