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;
}
}
輸出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MyRootElement>
<nameAddress>ROSS GZ</nameAddress>
<address>GZ</address>
<name>ROSS</name>
</MyRootElement>
可以看到element的順序和propOrder里指定的一樣