JAXB--@XmlElementWrapper注解(二)

在JAXB标准中,@XmlElementWrapper注解表示生成一个包装 XML 表示形式的包装器元素。 此元素主要用于生成一个包装集合的包装器 XML 元素。因此,该注释支持以下两种形式的序列化。

 注:@XmlElementWrapper仅允许出现在集合属性上。

第一步:定义将要转化的Java对象

  1. package step1.wrapper;  
  2.   
  3. import javax.xml.bind.annotation.XmlAttribute;  
  4. import javax.xml.bind.annotation.XmlElement;  
  5. import javax.xml.bind.annotation.XmlElementWrapper;  
  6. import javax.xml.bind.annotation.XmlRootElement;  
  7.   
  8. @XmlRootElement  
  9. public class Customer {  
  10.     String[] names;  
  11.     int age;  
  12.     int id;  
  13.       
  14.     //使用@XmlElementWrapper注解后,将会在原xml结点上再包装一层xml  
  15.     @XmlElementWrapper(name="allnames")  
  16.     @XmlElement(name="myname")  
  17.     public String[] getNames() {  
  18.         return names;  
  19.     }  
  20.     public void setNames(String[] names) {  
  21.         this.names = names;  
  22.     }  
  23.       
  24.     @XmlElement  
  25.     public int getAge() {  
  26.         return age;  
  27.     }  
  28.     public void setAge(int age) {  
  29.         this.age = age;  
  30.     }  
  31.       
  32.     @XmlAttribute  
  33.     public int getId() {  
  34.         return id;  
  35.     }  
  36.     public void setId(int id) {  
  37.         this.id = id;  
  38.     }  
  39.       
  40.     @Override  
  41.     public String toString() {  
  42.         return "Customer [id=" + id + ",names=" + names + ",age=" + age + "]";  
  43.     }  
  44. }  

第二步:编组

  1. package step1.wrapper;  
  2.   
  3. import java.io.File;  
  4. import javax.xml.bind.JAXBContext;  
  5. import javax.xml.bind.JAXBException;  
  6. import javax.xml.bind.Marshaller;  
  7.   
  8. //Marshaller  
  9. public class Object2XmlDemo {  
  10.     public static void main(String[] args) {  
  11.   
  12.         Customer customer = new Customer();  
  13.         customer.setId(100);  
  14.         customer.setNames(new String[]{"name-a","name-b","name-c"});  
  15.         customer.setAge(29);  
  16.         try {  
  17.             File file = new File("C:\file.xml");  
  18.             JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);  
  19.             Marshaller jaxbMarshaller = jaxbContext.createMarshaller();  
  20.             // output pretty printed  
  21.             jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
  22.             jaxbMarshaller.marshal(customer, file);  
  23.             jaxbMarshaller.marshal(customer, System.out);  
  24.         } catch (JAXBException e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.     }  
  28. }  

生成的xml:

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <customer id="100">  
  3.     <age>29</age>  
  4.     <allnames>  
  5.         <myname>name-a</myname>  
  6.         <myname>name-b</myname>  
  7.         <myname>name-c</myname>  
  8.     </allnames>  
  9. </customer>  
原文地址:https://www.cnblogs.com/toSeeMyDream/p/7824971.html