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://blog.csdn.net/woshixuye/article/details/13613805
分享到:
相关推荐
JAXB工具类 xml转为java对象 java对象转为xml ,本人亲测,可以使用!!!
NULL 博文链接:https://luyuwww.iteye.com/blog/1988355
该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用...
NULL 博文链接:https://xiongpan0807.iteye.com/blog/1832124
使用java jdk的JAXB技术实现xml与java对象互相转化代码教程: JDK中JAXB相关的重要Class和Interface:(来源于百度百科JAXB) •JAXBContext类,是应用的入口,用于管理XML/Java绑定信息。 •Marshaller接口,将Java...
NULL 博文链接:https://dacoolbaby.iteye.com/blog/1667283
jaxb将xml转换为对象或将对象转换为xml。测试通过。
THE Java™Architecture for XML Binding (JAXB) provides a fast and convenient way to bind between XML schemas and Java representations, making it easy for Java developers to incorporate XML data and ...
本代码主要是Java对象和Xml之间的转换实例代码,如果想具体指导代码是如何搭建的,可以参照http://blog.csdn.net/songdeitao/article/details/17304395这篇博文,这里的代码是和此对应的。
JAXB XML TO JAVA,文件转化 生成java代码
使用jaxb 实现xml——bean互转
JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。JAXB与xml相互转换实例。
jaxb 将xml里面的对象转化为一个个类,大大地简化了xml的相关操作。unmarshal marshal
NULL 博文链接:https://rayoo.iteye.com/blog/1233534
使用jaxb生成XML例子,含有例子和注解解析
用JAXB的xjc生成的Java类代表了在XML模式中不同的元素和复杂类型(complexType)。(一个复杂类型通过指定属性和元素内的元素来提供对一个元素的限定)。一个符合XML模式的XML文档可以从这些Java类中构建出来。 在这...
NULL 博文链接:https://zw7534313.iteye.com/blog/2422138
java 使用 JAXB 将xml转换为 bean 包含xml和dto和读取文件的util类
[精品]JAVA6新特性介绍使用JAXB来实现对象与XML之间的映射
编写xsd文件,利用jaxb生成java类。