时间:2021-05-19
在使用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) //访问类型改为字段@XmlRootElementpublic 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; }@Overridepublic 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() {}@Overridepublic 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; }}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互相转换方法与实例请查看下面的相关链接
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Jaxb2实现JavaBean与xml互转的方法。分享给大家供大家参考,具体如下:一、简介JAXB(JavaArchitectureforXMLB
xml与Java对象的转换详解1.xstream解析报文XStreamComponentx=XStreamComponent.newInstance();x.p
本文实例讲述了JavaBean与xml互相转换的方法。分享给大家供大家参考,具体如下:XML和JavaBean互相转换是一个很有用的功能,因为两者有着前后合作的
本文实例分析了C#编程实现对象与JSON串互相转换的方法。分享给大家供大家参考,具体如下:DoNet2.0需要借助于Newtonsoft.Json.dll代码如
本文实例讲述了Java利用序列化实现对象深度clone的方法。分享给大家供大家参考。具体实现方法如下:ByteArrayOutputStreambyteOut=