阅读(775) (1)

使用注解 @XmlList

2018-12-13 23:12:11 更新

使用注解 @XmlList

在JAXB 中,有一个注解 @XmlList主要是为了在一个XML的Element中添加多个值。

@XmlAccessorType(XmlAccessType.FIELD)
public class Product4 {


    @XmlAttribute
    private String id;

    
    @XmlList
    private List<String> item;
//  setters,getters
}

测试一下。

    @Test
    public void test4() throws JAXBException {
        Product4 product = new Product4();
        product.setId("1304");
        product.setItem(Arrays.asList("ItemA","ItemB","ItemC"));

        
        JAXB.marshal(product, System.out);
    }

生成的XMl 如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<product4 id="1304">
    <item>ItemA ItemB ItemC</item>
</product4>

可以看到,item包含了List中的所有数据。