答案 0 :(得分:1)
1)如果没有提供注释:
每个公共getter / setter对和每个公共领域都将是
自动绑定到XML,除非通过{@link XmlTransient}注释
例如
public class Cat
{
public String name = "tomcat";
private String nick = "catalina";
public int getAge() { return 5; }
public void setAge(int age) {}
}
之后
JAXB.marshal(cat, System.out);
输出
<cat>
<name>tomcat</name>
<age>5</age>
</cat>
2)XML属性怎么样? XML属性被命名为field或getter / setter对,并放置在根节点中 例如
@XmlAccessorType(XmlAccessType.FIELD)
public class Cat
{
String name = "tomcat";
@XmlAttribute
String nick = "catalina";
@XmlAttribute
String home = "java.home";
int age = 5;
}
输出
<cat home="java.home" nick="catalina">
<name>tomcat</name>
<age>5</age>
</cat>