默認(rèn)情況下彬呻,只有public 屬性才會(huì)綁定到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 對(duì)
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>