JAXB学习一 (入门)

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应用运行即可。

接受许可之后,在运行的当前目录下就会生成一个文件夹,结构大致为:

jaxb

这样就算安装成功了。

二、生成模型

安装完以后,就可以开始使用了,首先我们需要有一份schema文件,例如:

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.liulutu.com/students/"  
  3.     targetNamespace="http://www.liulutu.com/students/">  
  4.     <element name="students">  
  5.         <complexType>  
  6.             <sequence>  
  7.                 <element name="student" type="tns:StudentType" maxOccurs="unbounded" />  
  8.             </sequence>  
  9.         </complexType>  
  10.     </element>  
  11.     <simpleType name="SexType">  
  12.         <restriction base="string">  
  13.             <enumeration value="Male"></enumeration>  
  14.             <enumeration value="Female"></enumeration>  
  15.         </restriction>  
  16.     </simpleType>  
  17.   
  18.     <complexType name="StudentType">  
  19.         <attribute name="sex" type="tns:SexType"></attribute>  
  20.         <attribute name="name" type="string"></attribute>  
  21.     </complexType>  
  22. </schema>   

然后就可以根据这个schema文件生成对应的java模型类文件,可以到jaxb的bin目录下去,使用以下命令生成模型文件:

Java代码 复制代码  收藏代码
  1. xjc.bat students.xsd -d src -p com.liulutu.student.model  
xjc.bat students.xsd -d src -p com.liulutu.student.model

其中students.xsd指定要读入的schema文件;-d指定源代码存放目录;-p指定模型对象所在的包。

如果不出意外,上面的schema会对应生成以下模型文件:

models

三、使用

有了以上模型文件后,就可以开始使用,例如

  • 模型到XML
Java代码 复制代码  收藏代码
  1. public class TestMarshaller {   
  2.   
  3.     public static void main(String[] args) throws JAXBException {   
  4.         JAXBContext con = JAXBContext.newInstance("com.liulutu.student.model");   
  5.   
  6.         ObjectFactory factory = new ObjectFactory();   
  7.         Students students = factory.createStudents();   
  8.         addNewStudentTo(factory, students, "aaa", SexType.MALE);   
  9.         addNewStudentTo(factory, students, "bbb", SexType.FEMALE);   
  10.   
  11.         Marshaller marshaller = con.createMarshaller();   
  12.         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);   
  13.         marshaller.marshal(students, new File("a.xml"));   
  14.     }   
  15.   
  16.     private static void addNewStudentTo(ObjectFactory factory,   
  17.             Students students, String name, SexType type) {   
  18.         StudentType studentType = factory.createStudentType();   
  19.         studentType.setName(name);   
  20.         studentType.setSex(type);   
  21.         students.getStudent().add(studentType);   
  22.     }   
  23.   
  24. }  

 保存后的xml文件内容如下:

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <ns2:students xmlns:ns2="http://www.liulutu.com/students/">  
  3.     <student name="aaa" sex="Male"/>  
  4.     <student name="bbb" sex="Female"/>  
  5. </ns2:students>  
  •  XML到模型

以下代码用来还原以上保存的XML文件对应的模型(假如保存的文件名为a.xml):

Java代码 复制代码  收藏代码
  1. public class TestUnmarshaller {   
  2.   
  3.     public static void main(String[] args) throws JAXBException {   
  4.         JAXBContext con = JAXBContext.newInstance("com.liulutu.student.model");   
  5.         Unmarshaller unmarshaller = con.createUnmarshaller();   
  6.         Students students = (Students) unmarshaller.unmarshal(new File("a.xml"));   
  7.         List<StudentType> student = students.getStudent();   
  8.         Iterator<StudentType> iterator = student.iterator();   
  9.         while(iterator.hasNext()){   
  10.             StudentType next = iterator.next();   
  11.             System.out.println(next.getName()+"  "+next.getSex());   
  12.         }   
  13.     }   
  14.   
  15. }  

猜你喜欢

转载自zzc1684.iteye.com/blog/1674395