XmlAttribute

@XmlAttribute绑定一个成员变量到xml的元素属性

package com.test;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="MyRootElement")
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlAttributeTest {
    @XmlAttribute(name="myattribute")
    private String name;
    private String address;
    

    public static void main(String[] args) throws Exception {
        XmlAttributeTest root = new XmlAttributeTest();
        root.setName("ROSS");
        root.setAddress("GZ");
        
        JAXBContext context = JAXBContext.newInstance(XmlAttributeTest.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(root, System.out);
    }

    public void setName(String name) {
        this.name = name;
    }
    public void setAddress(String address) {
        this.address = address;
    }   
}

输出



    
GZ

你可能感兴趣的:(XmlAttribute)