JDK自带XML和java对象相互转换
2016-04-01 18:28
411 查看
下面使用的是JDK自带的类,没有引用任何第三方jar包。 Unmarshaller 类使客户端应用程序能够将 XML 数据转换为 Java 内容对象树。备注:marshal(序列化、排列、整理)Marshaller 类使客户端应用程序能够将 Java 内容树转换回 XML 数据。
参考博文:
http://hbiao68.iteye.com/blog/1958413
package hb.jaxb; import javax.xml.bind.annotation.XmlRootElement; //1、需要转换的model对象一定要添加@XmlRootElement注解,其里面的其他对象(Classroom)则不需要 @XmlRootElement public class Student { private int id; private String name; private int age; private Classroom classroom; 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 Classroom getClassroom() { return classroom; } public void setClassroom(Classroom classroom) { this.classroom = classroom; } public Student(int id, String name, int age, Classroom classroom) { super(); this.id = id; this.name = name; this.age = age; this.classroom = classroom; } //无参够着函数一定需要,否则JXBContext无法正常解析。 public Student() { super(); } }
package hb.jaxb; //2、需要转换的model对象一定要有不带参数的构造方法,包括该对象里面引用的对象。 public class Classroom { private int id; private String name; private int grade; 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 getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public Classroom(int id, String name, int grade) { super(); this.id = id; this.name = name; this.grade = grade; } public Classroom() { super(); } }
package hb.jaxb; import java.io.StringReader; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import org.junit.Test; //Eclipse console //中文乱码解决 eclipse-->Run-->Run Configuration-->Common--> Console Encoding-->Others-->UTF-8 //参考博文:http://hbiao68.iteye.com/blog/1958413 public class TestJaxb { @Test public void beanToXML() { Classroom classroom = new Classroom(1, "软件工程", 4); Student student = new Student(101, "张三", 22, classroom); try { JAXBContext context = JAXBContext.newInstance(Student.class); Marshaller marshaller = context.createMarshaller(); marshaller.marshal(student, System.out); } catch (JAXBException e) { e.printStackTrace(); } } @Test public void XMLStringToBean(){ String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><student><age>22</age><classroom><grade>4</grade><id>1</id><name>软件工程</name></classroom><id>101</id><name>张三</name></student>"; try { JAXBContext context = JAXBContext.newInstance(Student.class); Unmarshaller unmarshaller = context.createUnmarshaller(); Student student = (Student)unmarshaller.unmarshal(new StringReader(xmlStr)); System.out.println(student.getAge()); System.out.println(student.getClassroom().getName()); } catch (JAXBException e) { e.printStackTrace(); } } }
参考博文:
http://hbiao68.iteye.com/blog/1958413
相关文章推荐
- freeMarker 遍历 多重 map
- unix 环境高级编程第三版 源码无法 make 成功的注意点
- HDU 1064Financial Management
- ORACLE 11G EM 配置命令及问题处理
- 安装Oracle EM 11g Fusion Middleware Control
- mac升级系统到10.10.Versions crash 不能用
- 自定义表盘,一个是关于canvas.rotate的运用,一个是关于Path,Math的运用
- Multiple markers at this line - The type org.xmlpull.v1.XmlPullParser cannot be resolved. It is ind
- Oracle中使用聚合函数
- ARIMA(p,d,q)模型-1-MA模型
- google map v2 显示美洲地图 小例子 android应用
- OXM框架-自主研发【支持xml转bean】【bean转xml】【通过注解即可使用哦】
- OXM框架-自主研发【支持xml转bean】【bean转xml】【通过注解即可使用哦】
- uptown funk 火星哥霸占BILLBOARD排行榜8周的嗨爆神曲
- hdoj5023A Corrupt Mayor's Performance Art【线段树区间更新】
- spring 基础回顾 tips02
- Android 编译过程分析(1)
- 安装nagios插件报错 configure: error: C preprocessor "/lib/cpp" fails sanity check
- excel转为xml导入testlink的python实现
- 0516-787 extendlv: Maximum allocation