JAXB--@XmlElementWrapper注解和泛型一起使用(三)

当java对象的某个属性使用泛型时,普通对象都没问题,但是遇到HashSet这种集合类封装的元素时,就会出现元素内容序列化不出来的问题,详见如下:

 

一、示例:

第一步:定义java对象

 

Java代码   收藏代码
  1. package step3;  
  2.   
  3. import javax.xml.bind.annotation.XmlAccessType;  
  4. import javax.xml.bind.annotation.XmlAccessorType;  
  5. import javax.xml.bind.annotation.XmlRootElement;  
  6.   
  7. @XmlRootElement  
  8. @XmlAccessorType(value = XmlAccessType.PROPERTY)  
  9. public class Customer<T> {  
  10.     String name;  
  11.     int age;  
  12.     int id;  
  13.     T t;  
  14.   
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.   
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22.   
  23.     public int getAge() {  
  24.         return age;  
  25.     }  
  26.   
  27.     public void setAge(int age) {  
  28.         this.age = age;  
  29.     }  
  30.   
  31.     public int getId() {  
  32.         return id;  
  33.     }  
  34.   
  35.     public void setId(int id) {  
  36.         this.id = id;  
  37.     }  
  38.   
  39.       
  40.     @Override  
  41.     public String toString() {  
  42.         return "Customer [id=" + id + ",name=" + name + ",age=" + age + ",t=" + t + "]";  
  43.     }  
  44.   
  45.     public T getT() {  
  46.         return t;  
  47.     }  
  48.   
  49.     public void setT(T t) {  
  50.         this.t = t;  
  51.     }  
  52. }  

 

 

 

Java代码   收藏代码
  1. package step3;  
  2. import javax.xml.bind.annotation.XmlAccessType;  
  3. import javax.xml.bind.annotation.XmlAccessorType;  
  4.   
  5. @XmlAccessorType(value = XmlAccessType.PROPERTY)  
  6. public class Book {  
  7.       
  8.     private String id;  
  9.     private String name;  
  10.     private float price;  
  11.       
  12.     public String getId() {  
  13.         return id;  
  14.     }  
  15.     public void setId(String id) {  
  16.         this.id = id;  
  17.     }  
  18.     public String getName() {  
  19.         return name;  
  20.     }  
  21.     public void setName(String name) {  
  22.         this.name = name;  
  23.     }  
  24.     public float getPrice() {  
  25.         return price;  
  26.     }  
  27.     public void setPrice(float price) {  
  28.         this.price = price;  
  29.     }  
  30.     @Override  
  31.     public String toString() {  
  32.         return "Book [id=" + id + ",name=" + name + ",price=" + price + "]";  
  33.     }  
  34. }  
 

第二步:编组(JAXBContext.newInstance(Customer.class,HashSet.class);方法添加了

HashSet的class对象,以提供给JAXBContext使用。

 

Java代码   收藏代码
  1. package step3;  
  2. import java.io.File;  
  3. import java.util.HashSet;  
  4.   
  5. import javax.xml.bind.JAXBContext;  
  6. import javax.xml.bind.JAXBException;  
  7. import javax.xml.bind.Marshaller;  
  8.   
  9. //Marshaller  
  10. public class Object2XmlDemo {  
  11.     public static void main(String[] args) {  
  12.   
  13.         Customer<HashSet<Book>> customer = new Customer<HashSet<Book>>();  
  14.         customer.setId(100);  
  15.         customer.setName("suo");  
  16.         customer.setAge(29);  
  17.           
  18.         Book book = new Book();  
  19.         book.setId("1");  
  20.         book.setName("哈里波特");  
  21.         book.setPrice(100);  
  22.           
  23.         Book book2 = new Book();  
  24.         book2.setId("2");  
  25.         book2.setName("苹果");  
  26.         book2.setPrice(50);  
  27.           
  28.         HashSet<Book> bookSet = new HashSet<Book>();  
  29.         bookSet.add(book);  
  30.         bookSet.add(book2);  
  31.           
  32.         customer.setT(bookSet);  
  33.           
  34.         try {  
  35.             File file = new File("C:\\file1.xml");  
  36.             JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class,  
  37.                     HashSet.class);  
  38.             Marshaller jaxbMarshaller = jaxbContext.createMarshaller();  
  39.             // output pretty printed  
  40.             jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
  41.             jaxbMarshaller.marshal(customer, file);  
  42.             jaxbMarshaller.marshal(customer, System.out);  
  43.         } catch (JAXBException e) {  
  44.             e.printStackTrace();  
  45.         }  
  46.     }  
  47. }  

 

得到的xml:

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <customer>  
  3.     <age>29</age>  
  4.     <id>100</id>  
  5.     <name>suo</name>  
  6.     <xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="hashSet"/>  
  7. </customer>  

 

 

注:

1.泛型使用集合元素替代时,泛型属性所对应的xml没有序列化出来。

2.若JAXBContext.newInstance(Customer.class,HashSet.class);不添加HashSet

的class对象,则报错:

[javax.xml.bind.JAXBException: class java.util.HashSet nor any of its super class is known to this context.]

 

解决办法:

第一步:新增HashSet的包装类

 

Book类和Customer类相关代码均不需要改变,新增一个HashSet的包装类,定义如下:

 

Java代码   收藏代码
  1. package step4;  
  2.   
  3. import java.util.HashSet;  
  4.   
  5. import javax.xml.bind.annotation.XmlElement;  
  6. import javax.xml.bind.annotation.XmlElementWrapper;  
  7.   
  8. public class BookSet {  
  9.       
  10.     private HashSet<Book> bookSet = new HashSet<Book>();  
  11.   
  12.     //仅包含get方法,未包含set方法  
  13.     @XmlElementWrapper(name = "bookSet")//该注解非必须,仅是标注集合元素  
  14.     @XmlElement(name="book")  
  15.     public HashSet<Book> getBookSet() {  
  16.         return bookSet;  
  17.     }  
  18.   
  19.     public void addBook(Book book){  
  20.         bookSet.add(book);  
  21.     }  
  22.       
  23. }  

 

 

注:

1.BookSet类内部使用HashSet实现.

2.BookSet类在get方法上添加了@XmlElementWrapper(name = "bookSet")注解。

 

第二步:编组

 

Java代码   收藏代码
  1. package step4;  
  2.   
  3. import java.io.File;  
  4.   
  5. import javax.xml.bind.JAXBContext;  
  6. import javax.xml.bind.JAXBException;  
  7. import javax.xml.bind.Marshaller;  
  8.   
  9. //Marshaller  
  10. public class Object2XmlDemo {  
  11.     public static void main(String[] args) {  
  12.   
  13.         Customer<BookSet> customer = new Customer<BookSet>();  
  14.         customer.setId(100);  
  15.         customer.setName("suo");  
  16.         customer.setAge(29);  
  17.           
  18.         Book book = new Book();  
  19.         book.setId("1");  
  20.         book.setName("哈里波特");  
  21.         book.setPrice(100);  
  22.           
  23.         Book book2 = new Book();  
  24.         book2.setId("2");  
  25.         book2.setName("苹果");  
  26.         book2.setPrice(50);  
  27.           
  28.         BookSet bookSet = new BookSet();  
  29.         bookSet.addBook(book);  
  30.         bookSet.addBook(book2);  
  31.           
  32.         customer.setT(bookSet);  
  33.           
  34.         try {  
  35.             File file = new File("C:\\file1.xml");  
  36.             JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class,  
  37.                     BookSet.class);  
  38.             Marshaller jaxbMarshaller = jaxbContext.createMarshaller();  
  39.             // output pretty printed  
  40.             jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
  41.             jaxbMarshaller.marshal(customer, file);  
  42.             jaxbMarshaller.marshal(customer, System.out);  
  43.         } catch (JAXBException e) {  
  44.             e.printStackTrace();  
  45.         }  
  46.     }  
  47. }  

 

 

注:

1.定义Customer对象时,使用包装类,即:

Customer<BookSet> customer = new Customer<BookSet>();

2.JAXBContext调用newInstance()方法时,传入BookSet的class对象,告知BookSet的类型,即:JAXBContext.newInstance(Customer.class,BookSet.class);

 

得到的xml:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <customer>  
  3.     <age>29</age>  
  4.     <id>100</id>  
  5.     <name>suo</name>  
  6.     <xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="bookSet">  
  7.         <bookSet>  
  8.             <book>  
  9.                 <id>2</id>  
  10.                 <name>苹果</name>  
  11.                 <price>50.0</price>  
  12.             </book>  
  13.             <book>  
  14.                 <id>1</id>  
  15.                 <name>哈里波特</name>  
  16.                 <price>100.0</price>  
  17.             </book>  
  18.         </bookSet>  
  19.     </t>  
  20. </customer>  
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值