日期:2014-05-20 浏览次数:20668 次
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
@XmlRootElement
public class Hierarchy {
private String code;
private String value;
@XmlAttribute(name="Hierarchy_Code")//这个注解说明为一个属性
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@XmlValue//此注解时说明这是一个元素的值不会生成一个元素或属性只是值
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public static void main(String[] args) throws Exception {
JAXBContext context=JAXBContext.newInstance(Hierarchy.class);
Marshaller marshaller=context.createMarshaller();
Hierarchy h=new Hierarchy();
h.setCode("TEST");
h.setValue("测试用例");
marshaller.marshal(h, new File("a.xml"));
}
}