XmlElementWrapper

XmlElementWrapper用在集合类的member上,把集合类包起来

package com.test;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="MyRootElement")
public class XmlWrapperTest {
    @XmlElement
    @XmlElementWrapper(name="wrapit")
    private List name;
    @XmlElement
    private String address;
    
    public XmlWrapperTest() {
        name = new ArrayList<>();
    }
    
    public void setName(String name) {
        this.name.add(name);
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public static void main(String[] args) throws Exception {
        XmlWrapperTest root = new XmlWrapperTest();
        root.setName("ROSS");
        root.setAddress("GZ");
        
        JAXBContext context = JAXBContext.newInstance(XmlWrapperTest.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(root, System.out);
    }
    
}

输出



    
        ROSS
    
    
GZ

你可能感兴趣的:(XmlElementWrapper)