您好,登錄后才能下訂單哦!
本文章向大家介紹怎么在java中利用JAXB將對象和xml進行轉換的基本知識點總結和需要注意事項,具有一定的參考價值,需要的朋友可以參考一下。
Java的特點有哪些 1.Java語言作為靜態面向對象編程語言的代表,實現了面向對象理論,允許程序員以優雅的思維方式進行復雜的編程。 2.Java具有簡單性、面向對象、分布式、安全性、平臺獨立與可移植性、動態性等特點。 3.使用Java可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序等。
在使用WebService實現數據上傳下載,數據查詢時,可以利用JAXB實現java實體類和xml互相轉換
包含list(set同理)、map、Teacher、Date 類型的屬性
package jaxb; import java.util.Date; import java.util.List; import java.util.Map; 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; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; @XmlType(propOrder={"id","name","age","birthDay","list","teacher","map"}) //指定序列成的xml節點順序 @XmlAccessorType(value=XmlAccessType.FIELD) //訪問類型改為字段 @XmlRootElement public class Student { @XmlElement private String id; @XmlElement private String name; @XmlElement private int age; @XmlJavaTypeAdapter(value=DateAdapter.class) @XmlElement private Date birthDay; @XmlElement private List<Teacher> list; @XmlElement private Teacher teacher; @XmlJavaTypeAdapter(value=MapAdapter.class) // @XmlElementWrapper(name="role") @XmlElement private Map map; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthDay() { return birthDay; } public void setBirthDay(Date birthDay) { this.birthDay = birthDay; } public List getList() { return list; } public void setList(List list) { this.list = list; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + ", birthDay=" + birthDay + ", list=" + list + ", teacher=" + teacher + ", map=" + map + "]"; } }
注意:Teacher 類不包含@XmlRootElement節點,因為目的是轉成一個XML文檔,根節點只有一個
package jaxb; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(value=XmlAccessType.FIELD) @XmlType(propOrder={"id","name","age"}) public class Teacher { @XmlElement private int id; @XmlElement private String name; @XmlElement private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Teacher(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public Teacher() { } @Override public String toString() { return "Teacher [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
自定義時間適配器類
在java實體類序列化xml日期時,需要對時間進行自定義轉換,jaxb提供了XmlAdapter,只要實現它后就可以自定義日期適配器
package jaxb; import java.text.SimpleDateFormat; import java.util.Date; import javax.xml.bind.annotation.adapters.XmlAdapter; public class DateAdapter extends XmlAdapter<String, Date> { //反序列化成日期對象Date @Override public Date unmarshal(String str) throws Exception { SimpleDateFormat format = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return str==null ? null:format.parse(str); } //序列化成xmL @Override public String marshal(Date date) throws Exception { SimpleDateFormat format = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return date==null ? "":format.format(date); } private SimpleDateFormat getSimpleDateFormat(String pattern){ SimpleDateFormat format = new SimpleDateFormat(pattern); return format; } }
用來封裝map元素
package jaxb; public class Entry { private String key; private Object value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } }
map 轉換器
本人在寫map轉換器之后,感覺跟jaxb 默認序列化的方式沒什么區別,目前還不知道在javb中需不需要特地寫map轉換器,希望有經驗的朋友分享下
package jaxb; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.bind.annotation.adapters.XmlAdapter; public class MapAdapter extends XmlAdapter<MapAdapter.AdapterMap,Map<String,Object>> { public static class AdapterMap { public List<Entry> entry =new ArrayList<Entry>(); } /** * 功能:xml->Map<String,Object> * @param adapterMap * @return * @throws Exception */ @Override public Map<String, Object> unmarshal(AdapterMap adapterMap) throws Exception { Map<String, Object> map = new HashMap<String, Object>(); List entry = adapterMap.entry; for(Entry en:adapterMap.entry){ map.put(en.getKey(), en.getValue()); } return map; } /** * 功能:map<String,Object> -> xml */ @Override public AdapterMap marshal(Map<String, Object> map) throws Exception { AdapterMap adapterMap = new AdapterMap(); for (Map.Entry<String, Object> entry : map.entrySet()) { Entry entry2 = new Entry(); entry2.setKey(entry.getKey()); entry2.setValue(entry.getValue()); adapterMap.entry.add(entry2); } return adapterMap; } }
package jaxb; import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class JaxbDemo { public static void main(String[] args) { Student stu = new Student(); stu.setId("1"); stu.setName("羅志茂"); stu.setAge(25); stu.setBirthDay(new Date()); //list集合,set集合一樣 ArrayList<Teacher> list = new ArrayList<Teacher>(); list.add(new Teacher(2,"曾利梅",22)); list.add(new Teacher(3,"黃伙林",22)); list.add(new Teacher(4,"潘志華",22)); stu.setList(list); //在javaBean中添加javaBean Teacher teacher = new Teacher(1,"宋瑤",22); stu.setTeacher(teacher); //添加map集合 HashMap<Object, Object> map = new HashMap<>(); map.put("1", "林樂賢"); map.put("2", "德甲"); stu.setMap(map); try { JAXBContext context = JAXBContext.newInstance(Student.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "GBK"); //marshaller.marshal(stu, System.out); StringWriter writer = new StringWriter(); marshaller.marshal(stu, writer); System.out.println(writer.toString()); //反序列化 Unmarshaller unmarshaller = context.createUnmarshaller(); StringReader reader = new StringReader(writer.toString()); Student stu2 = (Student) unmarshaller.unmarshal(reader); System.out.println("stu2"+stu2); } catch (JAXBException e) { e.getMessage(); e.printStackTrace(); } } }
<?xml version="1.0" encoding="GBK" standalone="yes"?> <student> <id>1</id> <name>羅志茂</name> <age>25</age> <birthDay>2016-08-18 23:07:26</birthDay> <list> <id>2</id> <name>曾利梅</name> <age>22</age> </list> <list> <id>3</id> <name>黃伙林</name> <age>22</age> </list> <list> <id>4</id> <name>潘志華</name> <age>22</age> </list> <teacher> <id>1</id> <name>宋瑤</name> <age>22</age> </teacher> <map> <entry> <key>2</key> <value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">德甲</value> </entry> <entry> <key>1</key> <value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">林樂賢</value> </entry> </map> </student>
stu2Student [id=1, name=羅志茂, age=25, birthDay=Thu Aug 18 23:07:26 CST 2016, list=[Teacher [id=2, name=曾利梅, age=22], Teacher [id=3, name=黃伙林, age=22], Teacher [id=4, name=潘志華, age=22]], teacher=Teacher [id=1, name=宋瑤, age=22], map={2=德甲, 1=林樂賢}]
以上就是小編為大家帶來的怎么在java中利用JAXB將對象和xml進行轉換的全部內容了,希望大家多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。