标签:
Jaxb2 实现JavaBean与xml互转 http://zhuchengzzcc.iteye.com/blog/1838702
1、
1 package utils; 2 3 import java.io.StringReader; 4 import java.io.StringWriter; 5 6 import javax.xml.bind.JAXBContext; 7 import javax.xml.bind.Marshaller; 8 import javax.xml.bind.Unmarshaller; 9 10 /** 11 * Jaxb2工具类 12 * @author zhuc 13 * @create 2013-3-29 下午2:40:14 14 */ 15 public class JaxbUtil { 16 17 /** 18 * JavaBean转换成xml 19 * 默认编码UTF-8 20 * @param obj 21 * @param writer 22 * @return 23 */ 24 public static String convertToXml(Object obj) { 25 return convertToXml(obj, "UTF-8"); 26 } 27 28 /** 29 * JavaBean转换成xml 30 * @param obj 31 * @param encoding 32 * @return 33 */ 34 public static String convertToXml(Object obj, String encoding) { 35 String result = null; 36 try { 37 JAXBContext context = JAXBContext.newInstance(obj.getClass()); 38 Marshaller marshaller = context.createMarshaller(); 39 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 40 marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); 41 42 StringWriter writer = new StringWriter(); 43 marshaller.marshal(obj, writer); 44 result = writer.toString(); 45 } catch (Exception e) { 46 e.printStackTrace(); 47 } 48 49 return result; 50 } 51 52 /** 53 * xml转换成JavaBean 54 * @param xml 55 * @param c 56 * @return 57 */ 58 @SuppressWarnings("unchecked") 59 public static <T> T converyToJavaBean(String xml, Class<T> c) { 60 T t = null; 61 try { 62 JAXBContext context = JAXBContext.newInstance(c); 63 Unmarshaller unmarshaller = context.createUnmarshaller(); 64 t = (T) unmarshaller.unmarshal(new StringReader(xml)); 65 } catch (Exception e) { 66 e.printStackTrace(); 67 } 68 69 return t; 70 } 71 }
2、简单转换
1 package t1; 2 3 import java.util.Date; 4 5 import javax.xml.bind.annotation.XmlAccessType; 6 import javax.xml.bind.annotation.XmlAccessorType; 7 import javax.xml.bind.annotation.XmlAttribute; 8 import javax.xml.bind.annotation.XmlElement; 9 import javax.xml.bind.annotation.XmlRootElement; 10 import javax.xml.bind.annotation.XmlType; 11 12 /** 13 * @author zhuc 14 * @create 2013-3-29 下午2:49:48 15 */ 16 @XmlAccessorType(XmlAccessType.FIELD) 17 @XmlRootElement 18 @XmlType(name = "book", propOrder = { "author", "calendar", "price", "id" }) 19 public class Book { 20 21 @XmlElement(required = true) 22 private String author; 23 24 @XmlElement(name = "price_1", required = true) 25 private float price; 26 27 @XmlElement 28 private Date calendar; 29 30 @XmlAttribute 31 private Integer id; 32 33 /** 34 * @return the author 35 */ 36 public String getAuthor() { 37 return author; 38 } 39 40 /** 41 * @return the price 42 */ 43 public float getPrice() { 44 return price; 45 } 46 47 /** 48 * @return the calendar 49 */ 50 public Date getCalendar() { 51 return calendar; 52 } 53 54 /** 55 * @return the id 56 */ 57 public Integer getId() { 58 return id; 59 } 60 61 /** 62 * @param author the author to set 63 */ 64 public void setAuthor(String author) { 65 this.author = author; 66 } 67 68 /** 69 * @param price the price to set 70 */ 71 public void setPrice(float price) { 72 this.price = price; 73 } 74 75 /** 76 * @param calendar the calendar to set 77 */ 78 public void setCalendar(Date calendar) { 79 this.calendar = calendar; 80 } 81 82 /** 83 * @param id the id to set 84 */ 85 public void setId(Integer id) { 86 this.id = id; 87 } 88 89 /* (non-Javadoc) 90 * @see java.lang.Object#toString() 91 */ 92 @Override 93 public String toString() { 94 return "Book [author=" + author + ", price=" + price + ", calendar=" + calendar + ", id=" + id + "]"; 95 } 96 97 }
1 package t1; 2 3 import java.util.Date; 4 5 import javax.xml.bind.JAXBException; 6 7 import org.junit.Test; 8 9 import utils.JaxbUtil; 10 11 /** 12 * @author zhuc 13 * @create 2013-3-29 下午2:50:00 14 */ 15 public class JaxbTest1 { 16 17 /** 18 * @throws JAXBException 19 */ 20 @Test 21 public void showMarshaller() { 22 Book book = new Book(); 23 book.setId(100); 24 book.setAuthor("James"); 25 book.setCalendar(new Date()); 26 book.setPrice(23.45f); //默认是0.0 27 28 String str = JaxbUtil.convertToXml(book); 29 System.out.println(str); 30 } 31 32 /** 33 * @throws JAXBException 34 */ 35 @Test 36 public void showUnMarshaller() { 37 String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + 38 "<book id=\"100\">" + 39 " <author>James</author>" + 40 " <calendar>2013-03-29T09:25:56.004+08:00</calendar>" + 41 " <price_1>23.45</price_1>" + 42 "</book>"; 43 44 Book book = JaxbUtil.converyToJavaBean(str, Book.class); 45 System.out.println(book); 46 } 47 }
3、类中包含复杂对象的转换
1 package t2; 2 3 import javax.xml.bind.annotation.XmlAccessType; 4 import javax.xml.bind.annotation.XmlAccessorType; 5 import javax.xml.bind.annotation.XmlAttribute; 6 import javax.xml.bind.annotation.XmlElement; 7 import javax.xml.bind.annotation.XmlRootElement; 8 import javax.xml.bind.annotation.XmlType; 9 10 /** 11 * @author zhuc 12 * @create 2013-3-29 下午2:51:44 13 */ 14 @XmlAccessorType(XmlAccessType.FIELD) 15 @XmlRootElement(name = "student") 16 @XmlType(propOrder = {}) 17 public class Student { 18 19 @XmlAttribute 20 private Integer id; 21 22 @XmlElement 23 private String name; 24 25 @XmlElement(name = "role") 26 private Role role; 27 28 /** 29 * @return the id 30 */ 31 public Integer getId() { 32 return id; 33 } 34 35 /** 36 * @return the name 37 */ 38 public String getName() { 39 return name; 40 } 41 42 /** 43 * @return the role 44 */ 45 public Role getRole() { 46 return role; 47 } 48 49 /** 50 * @param id the id to set 51 */ 52 public void setId(Integer id) { 53 this.id = id; 54 } 55 56 /** 57 * @param name the name to set 58 */ 59 public void setName(String name) { 60 this.name = name; 61 } 62 63 /** 64 * @param role the role to set 65 */ 66 public void setRole(Role role) { 67 this.role = role; 68 } 69 70 /* (non-Javadoc) 71 * @see java.lang.Object#toString() 72 */ 73 @Override 74 public String toString() { 75 return "Student [id=" + id + ", name=" + name + ", role=" + role + "]"; 76 } 77 78 }
1 package t2; 2 3 import javax.xml.bind.annotation.XmlAccessType; 4 import javax.xml.bind.annotation.XmlAccessorType; 5 import javax.xml.bind.annotation.XmlElement; 6 import javax.xml.bind.annotation.XmlType; 7 8 /** 9 * @author zhuc 10 * @create 2013-3-29 下午2:51:52 11 */ 12 @XmlAccessorType(XmlAccessType.FIELD) 13 @XmlType(propOrder = { "name", "desc" }) 14 public class Role { 15 16 @XmlElement 17 private String name; 18 19 @XmlElement 20 private String desc; 21 22 /** 23 * @return the name 24 */ 25 public String getName() { 26 return name; 27 } 28 29 /** 30 * @return the desc 31 */ 32 public String getDesc() { 33 return desc; 34 } 35 36 /** 37 * @param name the name to set 38 */ 39 public void setName(String name) { 40 this.name = name; 41 } 42 43 /** 44 * @param desc the desc to set 45 */ 46 public void setDesc(String desc) { 47 this.desc = desc; 48 } 49 50 /* (non-Javadoc) 51 * @see java.lang.Object#toString() 52 */ 53 @Override 54 public String toString() { 55 return "Role [name=" + name + ", desc=" + desc + "]"; 56 } 57 58 }
1 package t2; 2 3 import org.junit.Test; 4 5 import utils.JaxbUtil; 6 7 /** 8 * @author zhuc 9 * @create 2013-3-29 下午2:52:00 10 */ 11 public class JaxbTest2 { 12 13 @Test 14 public void showMarshaller() { 15 16 Student student = new Student(); 17 student.setId(12); 18 student.setName("test"); 19 20 Role role = new Role(); 21 role.setDesc("管理"); 22 role.setName("班长"); 23 24 student.setRole(role); 25 26 String str = JaxbUtil.convertToXml(student); 27 System.out.println(str); 28 } 29 30 @Test 31 public void showUnMarshaller() { 32 String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"+ 33 "<student id=\"12\">"+ 34 " <name>test</name>"+ 35 " <role>"+ 36 " <name>班长</name>"+ 37 " <desc>管理</desc>"+ 38 "</role>"+ 39 "</student>"; 40 Student student = JaxbUtil.converyToJavaBean(str, Student.class); 41 System.out.println(student); 42 } 43 44 }
4、集合对象的转换
1 package t3; 2 3 import java.util.List; 4 5 import javax.xml.bind.annotation.XmlAccessType; 6 import javax.xml.bind.annotation.XmlAccessorType; 7 import javax.xml.bind.annotation.XmlElement; 8 import javax.xml.bind.annotation.XmlElementWrapper; 9 import javax.xml.bind.annotation.XmlRootElement; 10 import javax.xml.bind.annotation.XmlType; 11 12 /** 13 * @author zhuc 14 * @create 2013-3-29 下午2:55:56 15 */ 16 @XmlAccessorType(XmlAccessType.FIELD) 17 @XmlRootElement(name = "country") 18 @XmlType(propOrder = { "name", "provinceList" }) 19 public class Country { 20 21 @XmlElement(name = "country_name") 22 private String name; 23 24 @XmlElementWrapper(name = "provinces") 25 @XmlElement(name = "province") 26 private List<Province> provinceList; 27 28 /** 29 * @return the name 30 */ 31 public String getName() { 32 return name; 33 } 34 35 /** 36 * @return the provinceList 37 */ 38 public List<Province> getProvinceList() { 39 return provinceList; 40 } 41 42 /** 43 * @param name the name to set 44 */ 45 public void setName(String name) { 46 this.name = name; 47 } 48 49 /** 50 * @param provinceList the provinceList to set 51 */ 52 public void setProvinceList(List<Province> provinceList) { 53 this.provinceList = provinceList; 54 } 55 56 /* (non-Javadoc) 57 * @see java.lang.Object#toString() 58 */ 59 @Override 60 public String toString() { 61 return "Country [name=" + name + ", provinceList=" + provinceList + "]"; 62 } 63 64 }
1 package t3; 2 3 import javax.xml.bind.annotation.XmlAccessType; 4 import javax.xml.bind.annotation.XmlAccessorType; 5 import javax.xml.bind.annotation.XmlElement; 6 import javax.xml.bind.annotation.XmlType; 7 8 /** 9 * @author zhuc 10 * @create 2013-3-29 下午2:56:03 11 */ 12 @XmlAccessorType(XmlAccessType.FIELD) 13 @XmlType(propOrder = { "name", "provCity" }) 14 public class Province { 15 16 @XmlElement(name = "province_name") 17 private String name; 18 19 @XmlElement(name = "prov_city") 20 private String provCity; 21 22 /** 23 * @return the provCity 24 */ 25 public String getProvCity() { 26 return provCity; 27 } 28 29 /** 30 * @param provCity the provCity to set 31 */ 32 public void setProvCity(String provCity) { 33 this.provCity = provCity; 34 } 35 36 /** 37 * @return the name 38 */ 39 public String getName() { 40 return name; 41 } 42 43 /** 44 * @param name the name to set 45 */ 46 public void setName(String name) { 47 this.name = name; 48 } 49 50 /* (non-Javadoc) 51 * @see java.lang.Object#toString() 52 */ 53 @Override 54 public String toString() { 55 return "Province [name=" + name + ", provCity=" + provCity + "]"; 56 } 57 58 }
1 package t3; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.junit.Test; 7 8 import utils.JaxbUtil; 9 10 /** 11 * @author zhuc 12 * @create 2013-3-29 下午2:56:11 13 */ 14 public class JaxbTest3 { 15 16 /** 17 * @throws JAXBException 18 */ 19 @Test 20 public void showMarshaller() { 21 Country country = new Country(); 22 country.setName("中国"); 23 24 List<Province> list = new ArrayList<Province>(); 25 Province province = new Province(); 26 province.setName("江苏省"); 27 province.setProvCity("南京市"); 28 Province province2 = new Province(); 29 province2.setName("浙江省"); 30 province2.setProvCity("杭州市"); 31 list.add(province); 32 list.add(province2); 33 34 country.setProvinceList(list); 35 36 String str = JaxbUtil.convertToXml(country); 37 System.out.println(str); 38 } 39 40 /** 41 * 42 */ 43 @Test 44 public void showUnMarshaller() { 45 String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"+ 46 "<country>"+ 47 " <country_name>中国</country_name>"+ 48 " <provinces>"+ 49 " <province>"+ 50 " <province_name>江苏省</province_name>"+ 51 " <prov_city>南京市</prov_city>"+ 52 " </province>"+ 53 " <province>"+ 54 " <province_name>浙江省</province_name>"+ 55 " <prov_city>杭州市</prov_city>"+ 56 " </province>"+ 57 " </provinces>"+ 58 "</country>"; 59 Country country = JaxbUtil.converyToJavaBean(str, Country.class); 60 System.out.println(country); 61 } 62 63 }
标签:
原文地址:http://www.cnblogs.com/wangwanchao/p/5164476.html