加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 05:14:57 所属栏目:百科 来源:网络整理
导读:JAXB(Java Architecture for XML Binding)是一种特殊的序列化/反序列化工具,可实现Java对象与XML的相互转换。 在JAXB中将一个Java对象――XML的过程称之为Marshal,XML――Java对象的过程称之UnMarshal。 @XmlRootElement public class SClass { private St

JAXB(Java Architecture for XML Binding)是一种特殊的序列化/反序列化工具,可实现Java对象与XML的相互转换。

在JAXB中将一个Java对象――>XML的过程称之为Marshal,XML――>Java对象的过程称之UnMarshal。

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

public SClass(String cnum,List<Student> students)
{
super();
this.cnum = cnum;
this.students = students;
}

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

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

public Student()
{
super();
}

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

public String getNum()
{
return num;
}

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

public String getName()
{
return name;
}

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

}

public class JaxbTest

{

@Test

public void test01() throws IOException

{

try

{

JAXBContext ctx = JAXBContext.newInstance(SClass.class);

Marshaller marshaller = ctx.createMarshaller();

List<Student> lstStudent = new ArrayList<Student>();

Student s1 = new Student("001","xy");

Student s2 = new Student("002","lw");

lstStudent.add(s1);

lstStudent.add(s2);

SClass sclass = new SClass("c001",lstStudent);


// 生成的XML文件位置

String path = "D:/sclass.xml";

File file = new File(path);

if (!file.exists())

{

file.createNewFile();

}

// 编码格式

marshaller.setProperty(Marshaller.JAXB_ENCODING,"gb2312");

// 是否格式化生成的XML

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);

// 是否省略XML头信息<?xml version="1.0" encoding="gb2312" standalone="yes"?>

marshaller.setProperty(Marshaller.JAXB_FRAGMENT,true);

// 生成

marshaller.marshal(sclass,file);

}

catch (JAXBException e)

{

e.printStackTrace();

}

}


@Test

public void test02() throws Exception

{

try

{

String path = "D:/sclass.xml";

InputStream is = new FileInputStream(path);

String content = IOUtils.getString(is);

JAXBContext ctx = JAXBContext.newInstance(SClass.class);

Unmarshaller um = ctx.createUnmarshaller();

SClass sclass = (SClass) um.unmarshal(new StringReader(content));

System.out.println(sclass.getCnum());

System.out.println(sclass.getStudents().get(1).getName());

}

catch (JAXBException e)

{

e.printStackTrace();

}

}

}

test01 执行结果:对象――>XML,生成XML标签的顺序按照首字母的顺序

<sClass>

<cnum>c001</cnum>

<students>

<name>xy</name>

<num>001</num>

</students>

<students>

<name>lw</name>

<num>002</num>

</students>

</sClass>


test02执行结果:

c001

lw

关于IOUtils可以参看我的博客:http://woshixy.blog.51cto.com/5637578/1316935

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读