JAXB Unmarshall列出

时间:2015-10-26 16:20:19

标签: java xml jaxb

我正在尝试将此XML解组为Java对象,这是一个包含EmailAdresses列表的Customer对象。

<customer>
 <emailAddresses>janed@example.com</emailAddresses>
 <emailAddresses>jdoe@example.org</emailAddresses>
</customer>

列表有问题,我得到了正确数量的列表项(2),但emailAddresses标记的值为null

Customer.java

@XmlRootElement( name = "customer" )
public class Customer
{
    private List<EmailAddress> emailAddresses;

    public Customer()
    {
        emailAddresses = new ArrayList<EmailAddress>();
    }

    public List<EmailAddress> getEmailAddresses()
    {
        return emailAddresses;
    }

    public void setEmailAddresses( List<EmailAddress> emailAddresses )
    {
        this.emailAddresses = emailAddresses;
    }
}

EmailAddress.java

public class EmailAddress
{
    private String emailAddresses;

    public String getEmailAddresses()
    {
        return emailAddresses;
    }

        public void setEmailAddresses( String emailAddresses )
    {
        this.emailAddresses = emailAddresses;
    }
}

单元测试失败

@Test
public void shouldDeserialiseCusomerXMLToObject() throws JAXBException
{
    String xml = "<customer>"
            + "    <emailAddresses>janed@example.com</emailAddresses>"
            + "    <emailAddresses>jdoe@example.org</emailAddresses>"
            + "</customer>";

    JAXBContext jaxbContext = JAXBContext.newInstance( Customer.class );
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    StringReader reader = new StringReader( xml );
    Customer msg = ( Customer ) jaxbUnmarshaller.unmarshal( reader );

    // This passes, I have 2 emailAddresses
    assertEquals( 2, msg.getEmailAddresses().size() );

    // This fails, I have a null pointer instead of the email address
    assertEquals( "janed@example.com", msg.getEmailAddresses().get( 0 ).getEmailAddresses() );
}

2 个答案:

答案 0 :(得分:3)

默认情况下,emailAddresses EmailAddress字段被视为子元素,期望XML为:

<customer>
    <emailAddresses>
        <emailAddresses>janed@example.com</emailAddresses>
    </emailAddresses>
    <emailAddresses>
        <emailAddresses>jdoe@example.org</emailAddresses>
    </emailAddresses>
</customer>

由于您的外部<emailAddresses>元素不包含内部<emailAddresses>元素,因此永远不会分配该字段。

您希望emailAddresses的{​​{1}}字段为(外部)EmailAddress元素的值,因此您必须通过指定<emailAddresses>告诉JAXB {3}}注释:

@XmlValue
public String getEmailAddresses()
{
    return emailAddresses;
}

@XmlValue结合使用时,@XmlValue注释特别有用,支持这样的XML:

<Person sex="male" age="25">John Doe</Person>

那里的课程将是:

public class Person {
    public enum Sex {
        @XmlEnumValue("male")   MALE,
        @XmlEnumValue("female") FEMALE,
    }
    @XmlAttribute
    private Sex sex;
    @XmlAttribute
    private int age;
    @Value
    private String name;
}

答案 1 :(得分:0)

您的电子邮件地址太多了。

如果您要将电子邮件地址列表更改为字符串列表,例如

@XmlRootElement( name = "customer" )
public class Customer
{
    private List<String> emailAddresses;

    public Customer()
    {
        emailAddresses = new ArrayList<String>();
    }

    public List<String> getEmailAddresses()
    {
        return emailAddresses;
    }

    public void setEmailAddresses( List<String> emailAddresses )
    {
        this.emailAddresses = emailAddresses;
    }
}