通过JAXB实现完成java对象和xml的互相转换
- - ITeye博客在项目中我们常常需要将java对象转为Xml,将xml转为java对象,这些繁琐的操作我们使用JAXB的话就可以很轻松的实现,一起看一个例子吧. 已有 0 人发表留言,猛击->> 这里<<-参与讨论. —软件人才免语言低担保 赴美带薪读研.
package com.xiongpan.model; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class ClassRoom { private String classid; private String classname; private List<Student> list= new ArrayList<Student>(); public String getClassid() { return classid; } public void setClassid(String classid) { this. classid = classid; } public String getClassname() { return classname; } public void setClassname(String classname) { this. classname = classname; } public List<Student> getList() { return list; } public void setList(List<Student> list) { this. list = list; } public ClassRoom(String classid, String classname, List<Student> list) { super(); this. classid = classid; this. classname = classname; this. list = list; } public ClassRoom() { super(); } @Override public String toString() { StringBuffer sb= new StringBuffer(); sb.append( "classid="+ this. classid+ " "); sb.append( "classname"+ this. classname+ " "); for (Student st : list) { sb.append(st.toString()); } return sb.toString(); } }
package com.xiongpan.model; public class Student { private String id; private String name; private int age; 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 Student(String id, String name, int age) { super(); this. id = id; this. name = name; this. age = age; } public Student() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
package com.xiongpan.model; import java.io.StringReader; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import org.junit.Test; public class MyTest { @Test public void testJaxbToXml(){ try { Student s1=new Student("1", "ggd1", 21); Student s2=new Student("2", "ggd2", 21); Student s3=new Student("3", "ggd3", 21); Student s4=new Student("4", "ggd4", 21); Student s5=new Student("5", "ggd5", 26); Student s6=new Student("6", "ggd6", 21); Student s7=new Student("7", "ggd7", 26); List<Student> list=new ArrayList<Student>(); list.add(s1); list.add(s2); list.add(s3); list.add(s4); list.add(s5); list.add(s6); list.add(s7); ClassRoom classRoom=new ClassRoom("131123-1", "bestClassRoom", list); JAXBContext ct=JAXBContext.newInstance(ClassRoom.class); Marshaller marshaller= ct.createMarshaller(); marshaller.marshal(classRoom, System.out); } catch (JAXBException e) { e.printStackTrace(); } } @Test public void TestjaxbToObject(){ try { String xml="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><classRoom><classid>131123-1</classid><classname>bestClassRoom</classname><list><age>21</age><id>1</id><name>ggd1</name></list><list><age>21</age><id>2</id><name>ggd2</name></list><list><age>21</age><id>3</id><name>ggd3</name></list><list><age>21</age><id>4</id><name>ggd4</name></list><list><age>26</age><id>5</id><name>ggd5</name></list><list><age>21</age><id>6</id><name>ggd6</name></list><list><age>26</age><id>7</id><name>ggd7</name></list></classRoom>"; JAXBContext jaxbContext=JAXBContext.newInstance(ClassRoom.class); Unmarshaller unmarshaller=jaxbContext.createUnmarshaller(); ClassRoom classRoom= (ClassRoom) unmarshaller.unmarshal(new StringReader(xml)); System.out.println(classRoom.toString()); } catch (JAXBException e) { e.printStackTrace(); } } }
testJaxbToXml执行结果 <?xml version="1.0" encoding="UTF-8" standalone="yes"?><classRoom><classid>131123-1</classid><classname>bestClassRoom</classname><list><age>21</age><id>1</id><name>ggd1</name></list><list><age>21</age><id>2</id><name>ggd2</name></list><list><age>21</age><id>3</id><name>ggd3</name></list><list><age>21</age><id>4</id><name>ggd4</name></list><list><age>26</age><id>5</id><name>ggd5</name></list><list><age>21</age><id>6</id><name>ggd6</name></list><list><age>26</age><id>7</id><name>ggd7</name></list></classRoom> // xml To java Object执行结果 classid=131123-1 classnamebestClassRoom Student [id=1, name=ggd1, age=21]Student [id=2, name=ggd2, age=21]Student [id=3, name=ggd3, age=21]Student [id=4, name=ggd4, age=21]Student [id=5, name=ggd5, age=26]Student [id=6, name=ggd6, age=21]Student [id=7, name=ggd7, age=26]