JAXB全称Java Architecture for XML Binding,是一个用于在XML和Java对象之间进行映射的规范。使用JAXB,可以自动的将一个XML文档映射成对应的Java对象,也可以将对象保存成XML格式。有很多其他的处理XML结构和对象之间映射的技术,这里只讨论JAXB。
一、安装
首先我们需要去下一份JAXB的实现,可以去SUN(现在的oracle)网站上去下载:http://jaxb.java.net/
下载的是一个份jar文件,可以使用命令"java -jar jaxb**.jar"运行该jar文件,或者在windows(如果是的话)选择做为java应用运行即可。
接受许可之后,在运行的当前目录下就会生成一个文件夹,结构大致为:
这样就算安装成功了。
二、生成模型
安装完以后,就可以开始使用了,首先我们需要有一份schema文件,例如:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.liulutu.com/students/"
targetNamespace="http://www.liulutu.com/students/">
<element name="students">
<complexType>
<sequence>
<element name="student" type="tns:StudentType" maxOccurs="unbounded" />
</sequence>
</complexType>
</element>
<simpleType name="SexType">
<restriction base="string">
<enumeration value="Male"></enumeration>
<enumeration value="Female"></enumeration>
</restriction>
</simpleType>
<complexType name="StudentType">
<attribute name="sex" type="tns:SexType"></attribute>
<attribute name="name" type="string"></attribute>
</complexType>
</schema>
然后就可以根据这个schema文件生成对应的java模型类文件,可以到jaxb的bin目录下去,使用以下命令生成模型文件:
xjc.bat students.xsd -d src -p com.liulutu.student.model
其中students.xsd指定要读入的schema文件;-d指定源代码存放目录;-p指定模型对象所在的包。
如果不出意外,上面的schema会对应生成以下模型文件:
三、使用
有了以上模型文件后,就可以开始使用,例如
public class TestMarshaller {
public static void main(String[] args) throws JAXBException {
JAXBContext con = JAXBContext.newInstance("com.liulutu.student.model");
ObjectFactory factory = new ObjectFactory();
Students students = factory.createStudents();
addNewStudentTo(factory, students, "aaa", SexType.MALE);
addNewStudentTo(factory, students, "bbb", SexType.FEMALE);
Marshaller marshaller = con.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(students, new File("a.xml"));
}
private static void addNewStudentTo(ObjectFactory factory,
Students students, String name, SexType type) {
StudentType studentType = factory.createStudentType();
studentType.setName(name);
studentType.setSex(type);
students.getStudent().add(studentType);
}
}
保存后的xml文件内容如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:students xmlns:ns2="http://www.liulutu.com/students/">
<student name="aaa" sex="Male"/>
<student name="bbb" sex="Female"/>
</ns2:students>
以下代码用来还原以上保存的XML文件对应的模型(假如保存的文件名为a.xml):
public class TestUnmarshaller {
public static void main(String[] args) throws JAXBException {
JAXBContext con = JAXBContext.newInstance("com.liulutu.student.model");
Unmarshaller unmarshaller = con.createUnmarshaller();
Students students = (Students) unmarshaller.unmarshal(new File("a.xml"));
List<StudentType> student = students.getStudent();
Iterator<StudentType> iterator = student.iterator();
while(iterator.hasNext()){
StudentType next = iterator.next();
System.out.println(next.getName()+" "+next.getSex());
}
}
}
- 大小: 1.5 KB
- 大小: 2.3 KB
分享到:
相关推荐
NULL 博文链接:https://rensanning.iteye.com/blog/2378681
用JAXB生成一个XML文档,一个XML 模式(Schema)用XML语法表达了一个XML文档的结构。J2EE的开发者也许会需要一个符合XML模式的XML文档。Java XML绑定架构(JAXB)提供了一个绑定编译器,xjc,来从一个XML模式中生成Java类...
jaxb学习资料
NULL 博文链接:https://lijie-insist.iteye.com/blog/2160015
很好的jaxb入门示例 把所有注解标签都试一遍基本就OK了
JAXB的使用JAXB的使用JAXB的使用JAXB的使用
NULL 博文链接:https://popomonkey.iteye.com/blog/688580
JAXB学习笔记 项目为了进行xml与java bean的互转,参考了各位网友的推荐,最后选型为JAXB,闲暇之余整理了一下笔记。废话不多说,上代码
无需安装,解压后即可。jaxb-api.jar,jaxb-xjc.jar,jaxb-impl.jar,activation.jar等相关jar包在lib文件夹下。
jaxb-api jaxb-impl jar
Jaxb annotation 使用 Jaxb annotation初步使用
JAXB的安装包及插件
资料比较完整:Jaxb2_技术指南,JAXB的使用入门,JAXB介绍.ppt,JAXBDemos
JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件
JAXB的HelloWorld源码,适合想学习JAXB技术的新手学习。
B, java web 项目要用一个可以正常启动的web项目(当然没用到jaxb的web项目),打印System.out.println(System.getProperty("java.endorsed.dirs"));即可,我的是java web项目,打出的是E:/tomcat6/common/endorsed ...
一个JAXB2的基础教程,教你快速运用JAXB通过XSD绑定XML和读写XML
JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术
java -jar JAXB2_20060607.jar On Windows, you can just double-click the jar file to execute. Release Notes Browse the release notes online, including what's new. Technical Support Please subscribe to...
使用jaxb生成XML例子,含有例子和注解解析