简单例子step2 Book.java
1 package step2; 2 3 import javax.xml.bind.annotation.XmlAccessType; 4 import javax.xml.bind.annotation.XmlAccessorType; 5 /**//* @XmlAccessorType此注释可控制类中的属性和字段的默认序列化 6 * value=XmlAccessType.PROPERTY表示JAXB 绑定类中的每个 7 *获取方法/设置方法对将会自动绑定到 XML 8 * */ 9 @XmlAccessorType(value = XmlAccessType.PROPERTY) 10 public class Book { 11 12 private String id; 13 private String name; 14 private float price; 15 16 public String getId() { 17 return id; 18 } 19 public void setId(String id) { 20 this.id = id; 21 } 22 public String getName() { 23 return name; 24 } 25 public void setName(String name) { 26 this.name = name; 27 } 28 public float getPrice() { 29 return price; 30 } 31 public void setPrice(float price) { 32 this.price = price; 33 } 34 @Override 35 public String toString() { 36 return "Book [id=" + id + ",name=" + name + ",price=" + price + "]"; 37 } 38 } 39 Customer.java
1 package step2; 2 import java.util.Set; 3 4 import javax.xml.bind.annotation.XmlElement; 5 import javax.xml.bind.annotation.XmlElementWrapper; 6 import javax.xml.bind.annotation.XmlRootElement; 7 import javax.xml.bind.annotation.XmlType; 8 9 @XmlRootElement 10 @XmlType(propOrder = { "id", "name", "age","book"}) 11 public class Customer { 12 String name; 13 int age; 14 int id; 15 Set<Book> book; 16 @XmlElement(name="name") 17 public String getName() { 18 return name; 19 } 20 21 public void setName(String name) { 22 this.name = name; 23 } 24 25 @XmlElement(name="age") 26 public int getAge() { 27 return age; 28 } 29 30 public void setAge(int age) { 31 this.age = age; 32 } 33 @XmlElement(name="id") 34 public int getId() { 35 return id; 36 } 37 38 public void setId(int id) { 39 this.id = id; 40 } 41 42 43 @Override 44 public String toString() { 45 return "Customer [id=" + id + ",name=" + name + ",age=" + age + ",book=" + book + "]"; 46 } 47 @XmlElementWrapper(name="books") 48 @XmlElement(name="book") 49 public Set<Book> getBook() { 50 return book; 51 } 52 53 public void setBook(Set<Book> book) { 54 this.book = book; 55 } 56 57 58 } 59 Object2XmlDemo.java
1 package step2; 2 3 import java.io.File; 4 import java.util.HashSet; 5 import java.util.Set; 6 7 import javax.xml.bind.JAXBContext; 8 import javax.xml.bind.JAXBException; 9 import javax.xml.bind.Marshaller; 10 11 //Marshaller 12 public class Object2XmlDemo { 13 public static void main(String[] args) { 14 15 Customer customer = new Customer(); 16 customer.setId(100); 17 customer.setName("suo"); 18 customer.setAge(29); 19 20 Book book = new Book(); 21 book.setId("1"); 22 book.setName("哈里波特"); 23 book.setPrice(100); 24 25 Book book2 = new Book(); 26 book2.setId("2"); 27 book2.setName("苹果"); 28 book2.setPrice(50); 29 30 Set<Book> bookSet = new HashSet<Book>(); 31 bookSet.add(book); 32 bookSet.add(book2); 33 34 customer.setBook(bookSet); 35 36 try { 37 File file = new File("H:\\Xfile24.xml"); 38 JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class); 39 Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 40 // output pretty printed 41 jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 42 jaxbMarshaller.marshal(customer, file); 43 jaxbMarshaller.marshal(customer, System.out); 44 } catch (JAXBException e) { 45 e.printStackTrace(); 46 } 47 } 48 } 49 执行效果
1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 2 <customer> 3 <id>100</id> 4 <name>suo</name> 5 <age>29</age> 6 <books> 7 <book> 8 <id>1</id> 9 <name>鍝堥噷娉㈢壒</name> 10 <price>100.0</price> 11 </book> 12 <book> 13 <id>2</id> 14 <name>鑻规灉</name> 15 <price>50.0</price> 16 </book> 17 </books> 18 </customer> 19
|
|
CALENDER
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|
常用链接
留言簿(3)
随笔分类(22)
随笔档案(76)
文章分类(12)
文章档案(17)
搜索
积分与排名
最新评论

评论排行榜
Powered By: 博客园 模板提供:沪江博客
|