如果对要生成的 xml 格式有点些许的限制,就会对生成xml的对象就需要进行些许控制,控制对象的一个最可行的办法就是用注解。
比较常用的几个:
@XmlRootElement:根节点
@XmlAttribute:该属性作为xml的attribute
@XmlElement:该属性作为xml的element,且可以增加属性(name="NewElementName"),那么生成的xml串的elment的标签是NewElementName
示例:
package test;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import test.bean.EleClassA;
import test.bean.EleClassB;
import test.bean.RootClass;
public class Test1 {
public static void main(String[] args) {
RootClass rc = new RootClass();
EleClassA a = new EleClassA();
EleClassB b = new EleClassB();
a.setAttrC("attrc");
a.setEleA("eleA");
a.setEleB("eleB");
b.setAttrPassword("attrPassword");
b.setAttrUserName("attrUsrName");
b.setEleCode("eleCode");
rc.setA(a);
rc.setB(b);
rc.setRoot("root");
rc.setRootA("rootA");
JAXBContext context;
try {
context = JAXBContext.newInstance(RootClass.class);
Marshaller mar = context.createMarshaller();
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
StringWriter writer = new StringWriter();
mar.marshal(rc, writer);
System.out.println(writer.toString());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
package test.bean;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="rootclass")
public class RootClass {
private EleClassA a;
private EleClassB b;
private String root;
private String rootA;
@XmlElement(name="eleClassA")
public EleClassA getA() {
return a;
}
public void setA(EleClassA a) {
this.a = a;
}
@XmlElement(name="EleclassA")
public EleClassB getB() {
return b;
}
public void setB(EleClassB b) {
this.b = b;
}
public String getRoot() {
return root;
}
public void setRoot(String root) {
this.root = root;
}
public String getRootA() {
return rootA;
}
public void setRootA(String rootA) {
this.rootA = rootA;
}
}
package test.bean;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
public class EleClassA {
private String eleA;
private String eleB;
private String attrC;
@XmlElement
public String getEleA() {
return eleA;
}
public void setEleA(String eleA) {
this.eleA = eleA;
}
@XmlElement(name="elebnewname")
public String getEleB() {
return eleB;
}
public void setEleB(String eleB) {
this.eleB = eleB;
}
@XmlAttribute()
public String getAttrC() {
return attrC;
}
public void setAttrC(String attrC) {
this.attrC = attrC;
}
}
package test.bean;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
public class EleClassB {
private String attrUserName;
private String attrPassword;
private String eleCode;
@XmlAttribute
public String getAttrUserName() {
return attrUserName;
}
public void setAttrUserName(String attrUserName) {
this.attrUserName = attrUserName;
}
@XmlAttribute(name="password")
public String getAttrPassword() {
return attrPassword;
}
public void setAttrPassword(String attrPassword) {
this.attrPassword = attrPassword;
}
@XmlElement
public String getEleCode() {
return eleCode;
}
public void setEleCode(String eleCode) {
this.eleCode = eleCode;
}
}
运行Test1类中main方法,执行结果:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rootclass>
<eleClassA attrC="attrc">
<eleA>eleA</eleA>
<elebnewname>eleB</elebnewname>
</eleClassA>
<EleclassA attrUserName="attrUsrName" password="attrPassword">
<eleCode>eleCode</eleCode>
</EleclassA>
<root>root</root>
<rootA>rootA</rootA>
</rootclass>