默认情况下,只有public 属性才会绑定到xml元素
package com.test;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="MyRootElement")
public class AccessorType {
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 {
AccessorType root = new AccessorType();
root.setName("ROSS");
root.setAddress("GZ");
JAXBContext context = JAXBContext.newInstance(AccessorType.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(root, System.out);
}
}
输出如下,可以看到只有public的name被绑定了,而private的address没有。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MyRootElement>
<name>ROSS</name>
</MyRootElement>
使用@XmlAccessorType(XmlAccessType.FIELD)可以让所有member都被绑定
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;
@XmlRootElement(name="MyRootElement")
@XmlAccessorType(XmlAccessType.FIELD)
public class AccessorType {
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 {
AccessorType root = new AccessorType();
root.setName("ROSS");
root.setAddress("GZ");
JAXBContext context = JAXBContext.newInstance(AccessorType.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(root, System.out);
}
}
输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MyRootElement>
<name>ROSS</name>
<address>GZ</address>
</MyRootElement>
除了绑定member,还可以用@XmlAccessorType(XmlAccessType.PROPERTY)绑定getter/setter 对
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;
@XmlRootElement(name="MyRootElement")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class AccessorType {
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 {
AccessorType root = new AccessorType();
root.setName("ROSS");
root.setAddress("GZ");
JAXBContext context = JAXBContext.newInstance(AccessorType.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) {}
}
输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MyRootElement>
<nameAddress>ROSS GZ</nameAddress>
</MyRootElement>