XmlType(propOrder={...})

XmlType(propOrder={...}) 可以指定xml element的顺序

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.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name="MyRootElement")
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(propOrder= {"nameAddress", "address", "name"})
public class XmlTypeTest {
    
    public String name;
    private String address;
    
    public void setName(String name) {
        this.name = name;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public static void main(String[] args) throws Exception {
        XmlTypeTest root = new XmlTypeTest();
        root.setName("ROSS");
        root.setAddress("GZ");
        
        JAXBContext context = JAXBContext.newInstance(XmlTypeTest.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(root, System.out);
    }
    public String getNameAddress() {
        return name + " " + address;
    }
    public void setNameAddress(String nameAddress) {}
    public String getName() {
        return name;
    }
    public String getAddress() {
        return address;
    }
}

输出



    ROSS GZ
    
GZ
ROSS

可以看到element的顺序和propOrder里指定的一样

你可能感兴趣的:(XmlType(propOrder={...}))