java利用JAXB实现对象和xml互相转换方法与实例详解
1、JAXB应用场景
在使用WebService实现数据上传下载,数据查询时,可以利用JAXB实现java实体类和xml互相转换
2、Demo
2.1student.java实体类
包含list(set同理)、map、Teacher、Date类型的属性
packagejaxb; importjava.util.Date; importjava.util.List; importjava.util.Map; importjavax.xml.bind.annotation.XmlAccessType; importjavax.xml.bind.annotation.XmlAccessorType; importjavax.xml.bind.annotation.XmlElement; importjavax.xml.bind.annotation.XmlElementWrapper; importjavax.xml.bind.annotation.XmlRootElement; importjavax.xml.bind.annotation.XmlType; importjavax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; @XmlType(propOrder={"id","name","age","birthDay","list","teacher","map"})//指定序列成的xml节点顺序 @XmlAccessorType(value=XmlAccessType.FIELD)//访问类型改为字段 @XmlRootElement publicclassStudent{ @XmlElement privateStringid; @XmlElement privateStringname; @XmlElement privateintage; @XmlJavaTypeAdapter(value=DateAdapter.class) @XmlElement privateDatebirthDay; @XmlElement privateListlist; @XmlElement privateTeacherteacher; @XmlJavaTypeAdapter(value=MapAdapter.class) //@XmlElementWrapper(name="role") @XmlElement privateMapmap; publicStringgetId(){ returnid; } publicvoidsetId(Stringid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } publicDategetBirthDay(){ returnbirthDay; } publicvoidsetBirthDay(DatebirthDay){ this.birthDay=birthDay; } publicListgetList(){ returnlist; } publicvoidsetList(Listlist){ this.list=list; } publicTeachergetTeacher(){ returnteacher; } publicvoidsetTeacher(Teacherteacher){ this.teacher=teacher; } publicMapgetMap(){ returnmap; } publicvoidsetMap(Mapmap){ this.map=map; } @Override publicStringtoString(){ return"Student[id="+id+",name="+name+",age="+age +",birthDay="+birthDay+",list="+list+",teacher=" +teacher+",map="+map+"]"; } }
2.2Teacher.java类
注意:Teacher类不包含@XmlRootElement节点,因为目的是转成一个XML文档,根节点只有一个
packagejaxb; importjavax.xml.bind.annotation.XmlAccessType; importjavax.xml.bind.annotation.XmlAccessorType; importjavax.xml.bind.annotation.XmlElement; importjavax.xml.bind.annotation.XmlType; @XmlAccessorType(value=XmlAccessType.FIELD) @XmlType(propOrder={"id","name","age"}) publicclassTeacher{ @XmlElement privateintid; @XmlElement privateStringname; @XmlElement privateintage; publicintgetId(){ returnid; } publicvoidsetId(intid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } publicTeacher(intid,Stringname,intage){ super(); this.id=id; this.name=name; this.age=age; } publicTeacher(){ } @Override publicStringtoString(){ return"Teacher[id="+id+",name="+name+",age="+age+"]"; } }
2.3DateApdapter.java
自定义时间适配器类
在java实体类序列化xml日期时,需要对时间进行自定义转换,jaxb提供了XmlAdapter,只要实现它后就可以自定义日期适配器
packagejaxb; importjava.text.SimpleDateFormat; importjava.util.Date; importjavax.xml.bind.annotation.adapters.XmlAdapter; publicclassDateAdapterextendsXmlAdapter{ //反序列化成日期对象Date @Override publicDateunmarshal(Stringstr)throwsException{ SimpleDateFormatformat=getSimpleDateFormat("yyyy-MM-ddHH:mm:ss"); returnstr==null?null:format.parse(str); } //序列化成xmL @Override publicStringmarshal(Datedate)throwsException{ SimpleDateFormatformat=getSimpleDateFormat("yyyy-MM-ddHH:mm:ss"); returndate==null?"":format.format(date); } privateSimpleDateFormatgetSimpleDateFormat(Stringpattern){ SimpleDateFormatformat=newSimpleDateFormat(pattern); returnformat; } }
2.4Entry.java类
用来封装map元素
packagejaxb; publicclassEntry{ privateStringkey; privateObjectvalue; publicStringgetKey(){ returnkey; } publicvoidsetKey(Stringkey){ this.key=key; } publicObjectgetValue(){ returnvalue; } publicvoidsetValue(Objectvalue){ this.value=value; } }
2.5、MapAdpter.java
map转换器
本人在写map转换器之后,感觉跟jaxb默认序列化的方式没什么区别,目前还不知道在javb中需不需要特地写map转换器,希望有经验的朋友分享下
packagejaxb; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importjava.util.Map; importjavax.xml.bind.annotation.adapters.XmlAdapter; publicclassMapAdapterextendsXmlAdapter>{ publicstaticclassAdapterMap{ publicList entry=newArrayList (); } /** *功能:xml->Map *@paramadapterMap *@return *@throwsException */ @Override publicMap unmarshal(AdapterMapadapterMap)throwsException{ Map map=newHashMap (); Listentry=adapterMap.entry; for(Entryen:adapterMap.entry){ map.put(en.getKey(),en.getValue()); } returnmap; } /** *功能:map ->xml */ @Override publicAdapterMapmarshal(Map map)throwsException{ AdapterMapadapterMap=newAdapterMap(); for(Map.Entry entry:map.entrySet()){ Entryentry2=newEntry(); entry2.setKey(entry.getKey()); entry2.setValue(entry.getValue()); adapterMap.entry.add(entry2); } returnadapterMap; } }
2.6测试类JaxbDemo.java
packagejaxb; importjava.io.StringReader; importjava.io.StringWriter; importjava.util.ArrayList; importjava.util.Date; importjava.util.HashMap; importjavax.xml.bind.JAXBContext; importjavax.xml.bind.JAXBException; importjavax.xml.bind.Marshaller; importjavax.xml.bind.Unmarshaller; publicclassJaxbDemo{ publicstaticvoidmain(String[]args){ Studentstu=newStudent(); stu.setId("1"); stu.setName("罗志茂"); stu.setAge(25); stu.setBirthDay(newDate()); //list集合,set集合一样 ArrayListlist=newArrayList (); list.add(newTeacher(2,"曾利梅",22)); list.add(newTeacher(3,"黄伙林",22)); list.add(newTeacher(4,"潘志华",22)); stu.setList(list); //在javaBean中添加javaBean Teacherteacher=newTeacher(1,"宋瑶",22); stu.setTeacher(teacher); //添加map集合 HashMap
3.测试结果
1 罗志茂 25 2016-08-1823:07:26
2 曾利梅 22
3 黄伙林 22
4 潘志华 22 1 宋瑶 22
stu2Student[id=1,name=罗志茂,age=25,birthDay=ThuAug1823:07:26CST2016,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互相转换方法与实例请查看下面的相关链接
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。