1

具有以下内容:

@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 始终为空,无论我放入了多少对象。谁能告诉我我做错了什么?

4

2 回答 2

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>
于 2013-05-29T15:42:52.863 回答
0

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

于 2013-09-24T10:39:49.543 回答