一、XmlElements的应用:(可用于子类和父类之间)
package my.test.xml.aaaxml; 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.XmlElement; import javax.xml.bind.annotation.XmlElements; import javax.xml.bind.annotation.XmlRootElement; public class Test { public static void main(String[] args) throws Exception { Address1 address1 = new Address1(); address1.setCity1("beijing"); Address2 address2 = new Address2(); address2.setCity2("beijing"); Dog dog = new Dog(); dog.setName("godName"); dog.setAddress(address2); JAXBContext context = JAXBContext.newInstance(Dog.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(dog, System.out); } } @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) class Dog { private String name; @XmlElements({ @XmlElement(name = "address1", type = Address1.class), @XmlElement(name = "address2", type = Address2.class), }) private Object address; public Object getAddress() { return address; } public void setAddress(Object address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } } @XmlAccessorType(XmlAccessType.FIELD) class Address1 { private String city1; public String getCity1() { return city1; } public void setCity1(String city1) { this.city1 = city1; } } @XmlAccessorType(XmlAccessType.FIELD) class Address2 { private String city2; public String getCity2() { return city2; } public void setCity2(String city2) { this.city2 = city2; } }
运行结果:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <dog> <name>godName</name> <address2> <city2>beijing</city2> </address2> </dog>
二、XmlElementRef、XmlSeeAlso、XmlRootElement的配合使用:(可用于子类和父类之间)
package my.test.xml.aaaxml; 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.XmlElementRef; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlSeeAlso; public class Test { public static void main(String[] args) throws Exception { Address1 address1 = new Address1(); address1.setCity1("beijing"); Address2 address2 = new Address2(); address2.setCity2("beijing"); Dog dog = new Dog(); dog.setName("godName"); dog.setAddress(address1); JAXBContext context = JAXBContext.newInstance(Dog.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(dog, System.out); } } @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) class Dog { private String name; @XmlElementRef private AbstractAddr address; public AbstractAddr getAddress() { return address; } public void setAddress(AbstractAddr address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } } @XmlSeeAlso({ Address1.class, Address2.class }) class AbstractAddr { } @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) class Address1 extends AbstractAddr { private String city1; public String getCity1() { return city1; } public void setCity1(String city1) { this.city1 = city1; } } @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) class Address2 extends AbstractAddr { private String city2; public String getCity2() { return city2; } public void setCity2(String city2) { this.city2 = city2; } }
运行结果:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <dog> <name>godName</name> <address1> <city1>beijing</city1> </address1> </dog>
三、XmlRootElement、XmlElementRef(可以用于父类和子类之间)
package my.test.xml.aaaxml; 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.XmlElementRef; import javax.xml.bind.annotation.XmlRootElement; public class Test { public static void main(String[] args) throws Exception { Address1 address1 = new Address1(); address1.setCity1("beijing"); Address2 address2 = new Address2(); address2.setCity2("beijing"); Dog dog = new Dog(); dog.setName("godName"); dog.setAddress(address1); JAXBContext context = JAXBContext.newInstance(Dog.class, dog.getAddress().getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(dog, System.out); } } @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) class Dog { private String name; @XmlElementRef private AbstractAddr address; public AbstractAddr getAddress() { return address; } public void setAddress(AbstractAddr address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } } // @XmlSeeAlso({ Address1.class, Address2.class }) class AbstractAddr { } @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) class Address1 extends AbstractAddr { private String city1; public String getCity1() { return city1; } public void setCity1(String city1) { this.city1 = city1; } } @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) class Address2 extends AbstractAddr { private String city2; public String getCity2() { return city2; } public void setCity2(String city2) { this.city2 = city2; } }
运行结果:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <dog> <name>godName</name> <address1> <city1>beijing</city1> </address1> </dog>
四、 动态的生成根节点元素名称(JAXBElement)
package my.test.xml.aaaxml; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElementRef; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.namespace.QName; public class Test { public static void main(String[] args) throws Exception { Address1 address1 = new Address1(); address1.setCity1("beijing"); Address2 address2 = new Address2(); address2.setCity2("beijing"); Dog dog = new Dog(); dog.setName("godName"); dog.setAddress(address1); JAXBContext context = JAXBContext.newInstance(Dog.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); QName qName = new QName("dogas"); JAXBElement<Dog> dogElement = new JAXBElement<Dog>(qName, Dog.class, dog); marshaller.marshal(dogElement, System.out); } } @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) class Dog { private String name; @XmlElementRef private AbstractAddr address; public AbstractAddr getAddress() { return address; } public void setAddress(AbstractAddr address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Dog [name=" + name + ", address=" + address + "]"; } } @XmlSeeAlso({ Address2.class, Address1.class }) class AbstractAddr { } @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) class Address1 extends AbstractAddr { private String city1; public String getCity1() { return city1; } public void setCity1(String city1) { this.city1 = city1; } @Override public String toString() { return "Address1 [city1=" + city1 + "]"; } } @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) class Address2 extends AbstractAddr { private String city2; public String getCity2() { return city2; } public void setCity2(String city2) { this.city2 = city2; } @Override public String toString() { return "Address2 [city2=" + city2 + "]"; } }
运行结果:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <dogas> <name>godName</name> <address1> <city1>beijing</city1> </address1> </dogas>
五、动态的生成根节点元素名称和非根节点元素名称(JAXBElement、XmlRegistry、XmlElementRef、XmlElementDecl)
package my.test.xml.aaaxml; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElementDecl; import javax.xml.bind.annotation.XmlElementRef; import javax.xml.bind.annotation.XmlRegistry; import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.namespace.QName; public class Test { public static void main(String[] args) throws Exception { Address1 address1 = new Address1(); address1.setCity1("beijing"); QName q1Name = new QName("addressas"); JAXBElement<?> address1Element = new JAXBElement<Address1>(q1Name, Address1.class, address1); Address2 address2 = new Address2(); address2.setCity2("beijing"); Dog dog = new Dog(); dog.setName("godName"); dog.setAddress((JAXBElement<AbstractAddr>) address1Element); JAXBContext context = JAXBContext.newInstance(Dog.class, ObjectFactory.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); QName qName = new QName("dogas"); JAXBElement<Dog> dogElement = new JAXBElement<Dog>(qName, Dog.class, dog); marshaller.marshal(dogElement, System.out); } } @XmlAccessorType(XmlAccessType.FIELD) class Dog { private String name; @XmlElementRef(name = "address") private JAXBElement<AbstractAddr> address; public JAXBElement<AbstractAddr> getAddress() { return address; } public void setAddress(JAXBElement<AbstractAddr> address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Dog [name=" + name + ", address=" + address + "]"; } } @XmlRegistry class ObjectFactory { @XmlElementDecl(name = "address") public JAXBElement<AbstractAddr> createAddress() { return null; } } @XmlSeeAlso({ Address2.class, Address1.class }) class AbstractAddr { } class Address1 extends AbstractAddr { private String city1; public String getCity1() { return city1; } public void setCity1(String city1) { this.city1 = city1; } @Override public String toString() { return "Address1 [city1=" + city1 + "]"; } } class Address2 extends AbstractAddr { private String city2; public String getCity2() { return city2; } public void setCity2(String city2) { this.city2 = city2; } @Override public String toString() { return "Address2 [city2=" + city2 + "]"; } }
运行结果:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <dogas> <name>godName</name> <addressas> <city1>beijing</city1> </addressas> </dogas>