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

{

private String cnum;

private List students;

public SClass()

{

super();

}

public SClass(String cnum,List students)

{

super();

this.cnum = cnum;

this.students = students;

}

public String getCnum()

{

return cnum;

}

public void setCnum(String cnum)

{

this.cnum = cnum;

}

public List getStudents()

{

return students;

}

public void setStudents(List 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 lstStudent = new ArrayList();

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

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

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标签的顺序按照首字母的顺序

c001

xy

001

xy2

002

test02执行结果:

c001

xy2

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

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值