2.Xml与多个对象的映射(聚合或组合)及注意事项

 在我们的实际应用中,Xml中的结构往往不止这么简单,一般都会有2,3层。也就是说如果映射成对象就是聚合(组合)的情况 。

就用我们上一章的例子继续来讲,简单我们的Book的author现在不止是一个String类型的名子,他是一个对象Author,并包含作者的相关个人信息。那我们怎么做列?

直接看代码

[java] view plain copy
 
  1. package com.jaxb.first;  
  2.   
  3. import javax.xml.bind.annotation.XmlAccessorType;  
  4. import javax.xml.bind.annotation.XmlRootElement;  
  5. import javax.xml.bind.annotation.XmlType;  
  6.   
  7. @XmlRootElement(name = "book")  
  8. // If you want you can define the order in which the fields are written  
  9. // Optional  
  10. @XmlType(propOrder = { "name", "author", "publisher", "isbn" })  
  11. public class Book {  
  12.   
  13.     private String name;  
  14.     private Author author;  
  15.     private String publisher;  
  16.     private String isbn;  
  17.   
  18.     // If you like the variable name, e.g. "name", you can easily change this  
  19.     // name for your XML-Output:  
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.   
  24.     public void setName(String name) {  
  25.         this.name = name;  
  26.     }  
  27.   
  28.   
  29.     public Author getAuthor() {  
  30.         return author;  
  31.     }  
  32.   
  33.     public void setAuthor(Author author) {  
  34.         this.author = author;  
  35.     }  
  36.   
  37.     public String getPublisher() {  
  38.         return publisher;  
  39.     }  
  40.   
  41.     public void setPublisher(String publisher) {  
  42.         this.publisher = publisher;  
  43.     }  
  44.   
  45.     public String getIsbn() {  
  46.         return isbn;  
  47.     }  
  48.   
  49.     public void setIsbn(String isbn) {  
  50.         this.isbn = isbn;  
  51.     }  
  52.   
  53. }  
[java] view plain copy
 
  1. package com.jaxb.first;  
  2.   
  3. import javax.xml.bind.annotation.XmlAttribute;  
  4.   
  5. public class Author {  
  6.   
  7.       
  8.     private String name;  
  9.     private int age;  
  10.       
  11.     @XmlAttribute  
  12.     public String getName() {  
  13.         return name;  
  14.     }  
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18.     public int getAge() {  
  19.         return age;  
  20.     }  
  21.     public void setAge(int age) {  
  22.         this.age = age;  
  23.     }  
  24.       
  25.       
  26.       
  27. }  



[java] view plain copy
 
  1. package com.jaxb.first;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import javax.xml.bind.annotation.XmlElement;  
  6. import javax.xml.bind.annotation.XmlElementWrapper;  
  7. import javax.xml.bind.annotation.XmlRootElement;  
  8.   
  9. @XmlRootElement(namespace="abc")  
  10. public class Bookstore {  
  11.   
  12.     // XmLElementWrapper generates a wrapper element around XML representation  
  13.     @XmlElementWrapper(name = "bookList")  
  14.     // XmlElement sets the name of the entities  
  15.     @XmlElement(name = "book")  
  16.     private ArrayList<Book> bookList;  
  17.     private String name;  
  18.     private String location;  
  19.   
  20.     public void setBookList(ArrayList<Book> bookList) {  
  21.         this.bookList = bookList;  
  22.     }  
  23.   
  24.     public ArrayList<Book> getBooksList() {  
  25.         return bookList;  
  26.     }  
  27.   
  28.     public String getName() {  
  29.         return name;  
  30.     }  
  31.   
  32.     public void setName(String name) {  
  33.         this.name = name;  
  34.     }  
  35.   
  36.     public String getLocation() {  
  37.         return location;  
  38.     }  
  39.   
  40.     public void setLocation(String location) {  
  41.         this.location = location;  
  42.     }  
  43. }  

 

[java] view plain copy
 
  1. package com.jaxb.first;  
  2.   
  3. import java.io.FileReader;  
  4. import java.io.FileWriter;  
  5. import java.io.IOException;  
  6. import java.io.Writer;  
  7. import java.util.ArrayList;  
  8.   
  9. import javax.xml.bind.JAXBContext;  
  10. import javax.xml.bind.JAXBException;  
  11. import javax.xml.bind.Marshaller;  
  12. import javax.xml.bind.Unmarshaller;  
  13.   
  14. import com.sun.xml.bind.marshaller.NamespacePrefixMapper;  
  15.   
  16. public class BookMain {  
  17.   
  18.     private static final String BOOKSTORE_XML = "./bookstore-jaxb.xml";  
  19.   
  20.     public static void main(String[] args) throws JAXBException, IOException {  
  21.   
  22.         ArrayList<Book> bookList = new ArrayList<Book>();  
  23.   
  24.         // create books  
  25.         Book book1 = new Book();  
  26.         book1.setIsbn("978-0060554736");  
  27.         book1.setName("The Game");  
  28.         Author a = new Author();  
  29.         a.setAge(28);  
  30.         a.setName("Gosling");  
  31.         book1.setAuthor(a);  
  32.         book1.setPublisher("Harpercollins");  
  33.         bookList.add(book1);  
  34.   
  35.         Book book2 = new Book();  
  36.         book2.setIsbn("978-3832180577");  
  37.         book2.setName("Feuchtgebiete");  
  38.         Author a2 = new Author();  
  39.         a2.setAge(32);  
  40.         a2.setName("James Green");  
  41.         book2.setAuthor(a2);  
  42.         book2.setPublisher("Dumont Buchverlag");  
  43.         bookList.add(book2);  
  44.   
  45.         // create bookstore, assigning book  
  46.         Bookstore bookstore = new Bookstore();  
  47.         bookstore.setName("Fraport Bookstore");  
  48.         bookstore.setLocation("Frankfurt Airport");  
  49.         bookstore.setBookList(bookList);  
  50.   
  51.         // create JAXB context and instantiate marshaller  
  52.         JAXBContext context = JAXBContext.newInstance(Bookstore.class);  
  53.         Marshaller m = context.createMarshaller();  
  54.         NamespacePrefixMapper mapper = new PreferredMapper();  
  55.         m.setProperty("com.sun.xml.bind.namespacePrefixMapper", mapper);  
  56.   
  57.         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);  
  58.         m.marshal(bookstore, System.out);  
  59.   
  60.         Writer w = null;  
  61.         try {  
  62.             w = new FileWriter(BOOKSTORE_XML);  
  63.             m.marshal(bookstore, w);  
  64.         } finally {  
  65.             try {  
  66.                 w.close();  
  67.             } catch (Exception e) {  
  68.             }  
  69.         }  
  70.   
  71.         // get variables from our xml file, created before  
  72.         System.out.println();  
  73.         System.out.println("Output from our XML File: ");  
  74.         Unmarshaller um = context.createUnmarshaller();  
  75.         Bookstore bookstore2 = (Bookstore) um.unmarshal(new FileReader(  
  76.                 BOOKSTORE_XML));  
  77.   
  78.         for (int i = 0; i < bookstore2.getBooksList().toArray().length; i++) {  
  79.             System.out.println("Book " + (i + 1) + ": "  
  80.                     + bookstore2.getBooksList().get(i).getName() + " from "  
  81.                     + bookstore2.getBooksList().get(i).getAuthor());  
  82.         }  
  83.   
  84.     }  
  85.   
  86.     public static class PreferredMapper extends NamespacePrefixMapper {  
  87.         @Override  
  88.         public String getPreferredPrefix(String namespaceUri,  
  89.                 String suggestion, boolean requirePrefix) {  
  90.             return "pre";  
  91.         }  
  92.   
  93.           
  94.     }  
  95. }  



看下输出结果:

[html] view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <pre:bookstore xmlns:pre="abc">  
  3.     <bookList>  
  4.         <book>  
  5.             <name>The Game</name>  
  6.             <author name="Gosling">  
  7.                 <age>28</age>  
  8.             </author>  
  9.             <publisher>Harpercollins</publisher>  
  10.             <isbn>978-0060554736</isbn>  
  11.         </book>  
  12.         <book>  
  13.             <name>Feuchtgebiete</name>  
  14.             <author name="James Green">  
  15.                 <age>32</age>  
  16.             </author>  
  17.             <publisher>Dumont Buchverlag</publisher>  
  18.             <isbn>978-3832180577</isbn>  
  19.         </book>  
  20.     </bookList>  
  21.     <location>Frankfurt Airport</location>  
  22.     <name>Fraport Bookstore</name>  
  23. </pre:bookstore>  
  24.   
  25. Output from our XML File:   
  26. Book 1: The Game from com.jaxb.first.Author@1774b9b  
  27. Book 2: Feuchtgebiete from com.jaxb.first.Author@104c575  

OK 是我们想要的格式吧。 那么事情就解决了

   值 得注意的是:如果你要对属性做注解,必须将注解写在属性的get方法上, 就如我们Author类中的 @XmlAttribute那样,否则运行的时候他会提示:the same element name xxx..

原文地址:https://www.cnblogs.com/sharpest/p/7878736.html