Jaxb2 实现 JavaBean 与 xml 互转
Java 代码??
package?t1;??
import?java.util.Date;??
import?javax.xml.bind.annotation.XmlAccessType;??
import?javax.xml.bind.annotation.XmlAccessorType;??
import?javax.xml.bind.annotation.XmlAttribute;??
import?javax.xml.bind.annotation.XmlElement;??
import?javax.xml.bind.annotat
ion.XmlRootElement;??
import?javax.xml.bind.annotation.XmlType;??
/**?
?*?@author??????zhuc?
?*?@create??????2013-3-29?下午 2:49:48?
?*/??
@XmlAccessorType(XmlAccessType.FIELD)??
@XmlRootElement??
@XmlType(name?=?"book",?propOrder?=?{?"author",?"calendar",?"price",?"id"?})??
public?class?Book?{??
????@XmlElement(required?=?true)??
????private?String?author;??
????@XmlElement(name?=?"price_1",?required?=?true)??
????private?float?price;??
????@XmlElement??
????private?Date?calendar;??
????@XmlAttribute??
????private?Integer?id;??
????/**?
?????*?@return?the?author?
?????*/??
????public?String?getAuthor()?{??
????????return?author;??
????}??
????/**?
?????*?@return?the?price?
?????*/??
????public?float?getPrice()?{??
????????return?price;??
????}??
????/**?
?????*?@return?the?calendar?
?????*/??
????public?Date?getCalendar()?{??
????????return?calendar;??
????}??
????/**?
?????*?@return?the?id?
?????*/??
????public?Integer?getId()?{??
????????return?id;??
????}??
????/**?
?????*?@param?author?the?author?to?set?
?????*/??
????public?void?setAuthor(String?author)?{??
????????this.author?=?author;??
????}??
????/**?
?????*?@param?price?the?price?to?set?
?????*/??
????public?void?setPrice(float?price)?{??
????????this.price?=?price;??
????}??
????/**?
?????*?@param?calendar?the?calendar?to?set?
?????*/??
????public?void?setCalendar(Date?calendar)?{??
????????this.calendar?=?calendar;??
????}??
????/**?
?????*?@param?id?the?id?to?set?
?????*/??
????public?void?setId(Integer?id)?{??
????????this.id?=?id;??
????}??
????/*?(non-Javadoc)?
?????*?@see?java.lang.Object#toString()?
?????*/??
????@Override??
????public?String?toString()?{??
????????return?"Book?[author="?+?author?+?",?price="?+?price?+?",?calendar="?+?calendar?+?",?id="?+?id?+?"]";??
????}??
}??
Java 代码??
package?t1;??
import?java.util.Date;??
import?javax.xml.bind.JAXBException;??
import?org.junit.Test;??
import?utils.JaxbUtil;??
/**?
?*?@author??????zhuc?
?*?@create??????2013-3-29?下午 2:50:00?
?*/??
public?class?JaxbTest1?{??
????/**?
?????*?@throws?JAXBException?
?????*/??
????@Test??
????public?void?showMarshaller()??{??
????????Book?book?=?new?Book();??
????????book.setId(100);??
????????book.setAuthor("James");??
????????book.setCalendar(new?Date());??
????????book.setPrice(23.45f);???//默认是 0.0??
????????String?str?=?JaxbUtil.convertToXml(book);??
????????System.out.println(str);??
????}??
????/**?
?????*?@throws?JAXBException?
?????*/??
????@Test??
????public?void?showUnMarshaller()?{??
????????String?str?=?"<?xml?version=\"1.0\"?encoding=\"UTF-8\"?standalone=\"yes\"?>"?+??
????????????"<book?id=\"100\">"?+??
????????????"????<author>James</author>"?+??
?????????????"???<calendar>2013-03-29T09:25:56.004+08:00</calendar>"?+??
??????????????"??<price_1>23.45</price_1>"?+??
????????????"</book>";??
????????Book?book?=?JaxbUtil.converyToJavaBean(str,?Book.class);??
????????System.out.println(book);??
????}??
}??
输出结果分别为:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
< book id="100">
<author>James</author>
<calendar>2013-03-29T14:50:58.974+08:00</calendar>
<price_1>23.45</price_1>
< /book>
Book [author=James, price=23.45, calendar=Fri Mar 29 09:25:56 CST 2013, id=100]
3、类中包含复杂对象的转换
Java 代码??
package?t2;??
import?javax.xml.bind.annotation.XmlAccessType;??
import?javax.xml.bind.annotation.XmlAccessorType;??
import?javax.xml.bind.annotation.XmlAttribute;??
import?javax.xml.bind.annotation.XmlElement;??
import?javax.xml.bind.annotation.XmlRootElement;??
import?javax.xml.bind.annotation.XmlType;??
/**?
?*?@author??????zhuc?
?*?@create??????2013-3-29?下午 2:51:44?
?*/??
@XmlAccessorType(XmlAccessType.FIELD)??
@XmlRootElement(name?=?"student")??
@XmlType(propOrder?=?{})??
public?class?Student?{??
????@XmlAttribute??
????private?Integer?id;??
????@XmlElement??
????private?String?name;??
????@XmlElement(name?=?"role")??
????private?Role?role;??
????/**?
?????*?@return?the?id?
?????*/??
????public?Integer?getId()?{??
????????return?id;??
????}??
????/**?
?????*?@return?the?name?
?????*/??
????public?String?getName()?{??
????????return?name;??
????}??
????/**?
?????*?@return?the?role?
?????*/??
????public?Role?getRole()?{??
????????return?role;??
????}??
????/**?
?????*?@param?id?the?id?to?set?
?????*/??
????public?void?setId(Integer?id)?{??
????????this.id?=?id;??
????}??
????/**?
?????*?@param?name?the?name?to?set?
?????*/??
????public?void?setName(String?name)?{??
????????this.name?=?name;??
????}??
????/**?
?????*?@param?role?the?role?to?set?
?????*/??
????public?void?setRole(Role?role)?{??
????????this.role?=?role;??
????}??
????/*?(non-Javadoc)?
?????*?@see?java.lang.Object#toString()?
?????*/??
????@Override??
????public?String?toString()?{??
????????return?"Student?[id="?+?id?+?",?name="?+?name?+?",?role="?+?role?+?"]";??
????}??
}??
Java 代码??
package?t2;??
import?javax.xml.bind.annotation.XmlAccessType;??
import?javax.xml.bind.annotation.XmlAccessorType;??
import?javax.xml.bind.annotation.XmlElement;??
import?javax.xml.bind.annotation.XmlType;??
/**?
?*?@author??????zhuc?
?*?@create??????2013-3-29?下午 2:51:52?
?*/??
@XmlAccessorType(XmlAccessType.FIELD)??
@XmlType(propOrder?=?{?"name",?"desc"?})??
public?class?Role?{??
????@XmlElement??
????private?String?name;??
????@XmlElement??
????private?String?desc;??
????/**?
?????*?@return?the?name?
?????*/??
????public?String?getName()?{??
????????return?name;??
????}??
????/**?
?????*?@return?the?desc?
?????*/??
????public?String?getDesc()?{??
????????return?desc;??
????}??
????/**?
?????*?@param?name?the?name?to?set?
?????*/??
????public?void?setName(String?name)?{??
????????this.name?=?name;??
????}??
????/**?
?????*?@param?desc?the?desc?to?set?
?????*/??
????public?void?setDesc(String?desc)?{??
????????this.desc?=?desc;??
????}??
????/*?(non-Javadoc)?
?????*?@see?java.lang.Object#toString()?
?????*/??
????@Override??
????public?String?toString()?{??
????????return?"Role?[name="?+?name?+?",?desc="?+?desc?+?"]";??
????}??
}??
Java 代码??
package?t2;??
import?org.junit.Test;??
import?utils.JaxbUtil;??
/**?
?*?@author??????zhuc?
?*?@create??????2013-3-29?下午 2:52:00?
?*/??
public?class?JaxbTest2?{??
????@Test??
????public?void?showMarshaller()?{??
????????Student?student?=?new?Student();??
????????student.setId(12);??
????????student.setName("test");??
????????Role?role?=?new?Role();??
????????role.setDesc("管理");??
????????role.setName("班长");??
????????student.setRole(role);??
????????String?str?=?JaxbUtil.convertToXml(student);??
????????System.out.println(str);??
????}??
????@Test??
????public?void?showUnMarshaller()?{??
????????String?str?=?"<?xml?version=\"1.0\"?encoding=\"UTF-8\"?standalone=\"yes\"?>"+??
????????????"<student?id=\"12\">"+??
????????????"????<name>test</name>"+??
?????????????"???<role>"+??
??????????????"??????<name>班长</name>"+??
???????????????"?????<desc>管理</desc>"+??
????????????????"</role>"+??
????????????"</student>";??
????????Student?student?=?JaxbUtil.converyToJavaBean(str,?Student.class);??
????????System.out.println(student);??
????}??
}??
输出结果分别为:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
< student id="12">
<name>test</name>
<role>
<name>班长</name>
<desc>管理</desc>
</role>
< /student>
Student [id=12, name=test, role=Role [name=班长, desc=管理]]
4、集合对象的转换(同样适用于 Set)
Java 代码??
package?t3;??
import?java.util.List;??
import?javax.xml.bind.annotation.XmlAccessType;??
import?javax.xml.bind.annotation.XmlAccessorType;??
import?javax.xml.bind.annotation.XmlElement;??
import?javax.xml.bind.annotation.XmlElementWrapper;??
import?javax.xml.bind.annotation.XmlRootElement;??
import?javax.xml.bind.annotation.XmlType;??
/**?
?*?@author??????zhuc?
?*?@create??????2013-3-29?下午 2:55:56?
?*/??
@XmlAccessorType(XmlAccessType.FIELD)??
@XmlRootElement(name?=?"country")??
@XmlType(propOrder?=?{?"name",?"provinceList"?})??
public?class?Country?{??
????@XmlElement(name?=?"country_name")??
????private?String?name;??
????@XmlElementWrapper(name?=?"provinces")??
????@XmlElement(name?=?"province")??
????private?List<Province>?provinceList;??
????/**?
?????*?@return?the?name?
?????*/??
????public?String?getName()?{??
????????return?name;??
????}??
????/**?
?????*?@return?the?provinceList?
?????*/??
????public?List<Province>?getProvinceList()?{??
????????return?provinceList;??
????}??
????/**?
?????*?@param?name?the?name?to?set?
?????*/??
????public?void?setName(String?name)?{??
????????this.name?=?name;??
????}??
????/**?
?????*?@param?provinceList?the?provinceList?to?set?
?????*/??
????public?void?setProvinceList(List<Province>?provinceList)?{??
????????this.provinceList?=?provinceList;??
????}??
????/*?(non-Javadoc)?
?????*?@see?java.lang.Object#toString()?
?????*/??
????@Override??
????public?String?toString()?{??
????????return?"Country?[name="?+?name?+?",?provinceList="?+?provinceList?+?"]";??
????}??
}??
Java 代码??
package?t3;??
import?javax.xml.bind.annotation.XmlAccessType;??
import?javax.xml.bind.annotation.XmlAccessorType;??
import?javax.xml.bind.annotation.XmlElement;??
import?javax.xml.bind.annotation.XmlType;??
/**?
?*?@author??????zhuc?
?*?@create??????2013-3-29?下午 2:56:03?
?*/??
@XmlAccessorType(XmlAccessType.FIELD)??
@XmlType(propOrder?=?{?"name",?"provCity"?})??
public?class?Province?{??
????@XmlElement(name?=?"province_name")??
????private?String?name;??
????@XmlElement(name?=?"prov_city")??
????private?String?provCity;??
????/**?
?????*?@return?the?provCity?
?????*/??
????public?String?getProvCity()?{??
????????return?provCity;??
????}??
????/**?
?????*?@param?provCity?the?provCity?to?set?
?????*/??
????public?void?setProvCity(String?provCity)?{??
????????this.provCity?=?provCity;??
????}??
????/**?
?????*?@return?the?name?
?????*/??
????public?String?getName()?{??
????????return?name;??
????}??
????/**?
?????*?@param?name?the?name?to?set?
?????*/??
????public?void?setName(String?name)?{??
????????this.name?=?name;??
????}??
评论