XML的操作——JAXB进行Java对象和XML之间的转换

前端之家收集整理的这篇文章主要介绍了XML的操作——JAXB进行Java对象和XML之间的转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
JAXB(Java Architecture for XML Binding)是一种特殊的序列化/反序列化工具,可实现Java对象与XML的相互转换。
在JAXB中将一个Java对象——>XML的过程称之为Marshal,XML——>Java对象的过程称之为UnMarshal。

@XmlRootElement
public class SClass
{
@H_301_8@
private String cnum;
@H_301_8@private List<Student> students;
@H_301_8@public SClass()
@H_301_8@{
@H_301_8@super();
@H_301_8@}

@H_301_8@public SClass(String cnum,List<Student> students)
@H_301_8@{
@H_301_8@super();
@H_301_8@this.cnum = cnum;
@H_301_8@this.students = students;
@H_301_8@}
@H_301_8@
@H_301_8@public String getCnum()
@H_301_8@{
@H_301_8@return cnum;
@H_301_8@}
@H_301_8@public void setCnum(String cnum)
@H_301_8@{
@H_301_8@this.cnum = cnum;
@H_301_8@}
@H_301_8@public List<Student> getStudents()
@H_301_8@{
@H_301_8@return students;
@H_301_8@}
@H_301_8@public void setStudents(List<Student> students)
@H_301_8@{
@H_301_8@this.students = students;
@H_301_8@}@H_301_8@
}


public class Student
{
@H_301_8@private String num;
@H_301_8@private String name;

@H_301_8@public Student()
@H_301_8@{
@H_301_8@super();
@H_301_8@}

@H_301_8@public Student(String num,String name)
@H_301_8@{
@H_301_8@super();
@H_301_8@this.num = num;
@H_301_8@this.name = name;
@H_301_8@}

@H_301_8@public String getNum()
@H_301_8@{
@H_301_8@return num;
@H_301_8@}

@H_301_8@public void setNum(String num)
@H_301_8@{
@H_301_8@this.num = num;
@H_301_8@}

@H_301_8@public String getName()
@H_301_8@{
@H_301_8@return name;
@H_301_8@}

@H_301_8@public void setName(String name)
@H_301_8@{
@H_301_8@this.name = name;
@H_301_8@}

}

public class JaxbTest
{
@H_301_8@
@Test
@H_301_8@public void test01() throws IOException
@H_301_8@{
@H_301_8@try
@H_301_8@{
@H_301_8@JAXBContext ctx = JAXBContext.newInstance(SClass.class);
@H_301_8@Marshaller marshaller = ctx.createMarshaller();
@H_301_8@List<Student> lstStudent = new ArrayList<Student>();
@H_301_8@Student s1 = new Student("001","xy");
@H_301_8@Student s2 = new Student("002","xy2");
@H_301_8@lstStudent.add(s1);
@H_301_8@lstStudent.add(s2);
@H_301_8@SClass sclass = new SClass("c001",lstStudent);

@H_301_8@// 生成的XML文件位置
@H_301_8@String path = "D:/sclass.xml";
@H_301_8@File file = new File(path);
@H_301_8@if (!file.exists())
@H_301_8@{
@H_301_8@file.createNewFile();
@H_301_8@}
@H_301_8@// 编码格式
@H_301_8@marshaller.setProperty(Marshaller.JAXB_ENCODING,"gb2312");
@H_301_8@// 是否格式化生成的XML
@H_301_8@marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
@H_301_8@// 是否省略XML头信息<?xml version="1.0" encoding="gb2312" standalone="yes"?>
@H_301_8@marshaller.setProperty(Marshaller.JAXB_FRAGMENT,true);
@H_301_8@// 生成
@H_301_8@marshaller.marshal(sclass,file);
@H_301_8@}
@H_301_8@catch (JAXBException e)
@H_301_8@{
@H_301_8@e.printStackTrace();
@H_301_8@}
@H_301_8@}

@H_301_8@@Test
@H_301_8@public void test02() throws Exception
@H_301_8@{
@H_301_8@try
@H_301_8@{
@H_301_8@String path = "D:/sclass.xml";
@H_301_8@InputStream is = new FileInputStream(path);
@H_301_8@String content = IoUtils.getString(is);
@H_301_8@JAXBContext ctx = JAXBContext.newInstance(SClass.class);
@H_301_8@Unmarshaller um = ctx.createUnmarshaller();
@H_301_8@SClass sclass = (SClass) um.unmarshal(new StringReader(content));
@H_301_8@System.out.println(sclass.getCnum());
@H_301_8@System.out.println(sclass.getStudents().get(1).getName());
@H_301_8@}
@H_301_8@catch (JAXBException e)
@H_301_8@{
@H_301_8@e.printStackTrace();
@H_301_8@}
@H_301_8@}

}

test01 执行结果:对象——>XML,生成XML标签的顺序按照首字母的顺序
<sClass>
<cnum>c001</cnum>
<students>
<name>xy</name>
<num>001</num>
</students>
<students>
<name>xy2</name>
<num>002</num>
</students>
</sClass>


test02执行结果:
c001
xy2


IoUtils可以参考我的博客:http://blog.csdn.net/woshixuye/article/details/13613805

猜你在找的XML相关文章