JAXB对象不会封送/解组List属性

时间:2013-05-29 10:19:28

标签: list jaxb marshalling unmarshalling

以下内容:

@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总是为空。谁能告诉我我做错了什么?

2 个答案:

答案 0 :(得分: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);
    }

}

我得到以下输出,因此list属性没有问题:

<?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>

答案 1 :(得分:0)

据我所知,您的问题是要解组您已编组的值列表。当解组导致空列表而XML包含至少1个元素时,我遇到了与jaxb-impl lib + 2.2.x相同的问题。如果在方法getListProps中为null,则尝试实例化列表,以便JAXB可以填充它。我觉得问题出在List + XmlAccessorType.PROPERTY中,因为它默认情况下不创建列表并尝试使用现有列表,因为它是null setListProps用空集合调用。