`
Josh_Persistence
  • 浏览: 1634143 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类

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 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会对应生成以下模型文件:

models

 

三、使用

 

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

  • 模型到XML
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到模型

 以下代码用来还原以上保存的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());
        }
    }

}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics