欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

JAXB教程,,首先,只要您稍微用过一点

来源: javaer 分享于  点击 13314 次 点评:252

JAXB教程,,首先,只要您稍微用过一点


首先,只要您稍微用过一点数据绑定,对这两个术语应该很熟悉。先回顾一下这两个术语,然后我们将讨论一些更有趣的术语。

编组(Marshalling)是把内存中的数据转化到存储媒介上的过程。因此在 Java 和 XML 环境中,编组就是把一些 Java 对象转化成一个(或多个) XML 文档。在数据库环境中,则是把 Java 表示的数据存入数据库。显然,编组的秘密在于把 Java 实例中的面向对象结构转化成适用于 XML 的 扁平结构,或者 RDBMS 中的关系结构(使用 Java 技术转换到 OODBMS 实际上很简单)。

解组(Unmarshalling)是把数据从存储媒介转换到内存中的过程--正好与编组相反。因此需要把 XML 文档解组到 Java VM 中。这里的复杂性不是在扁平数据中,因为这不是必需的,而在于从正确的数据到正确的 Java 代码变量的映射。如果映射是错误的,就不可能正确地访问数据。当然,如果再尝试重新编组还会造成更大的问题,并且问题传播得很快。

JAXB 的下载地址 http://jaxb.java.net/2.2.4/

使用 JAXB 进行之前,首先要生成表示 XML 数据的 Java 类。需要 XML Schema 以生成类和数据结构。

JAXB简单应用

<?xml version="1.0" encoding="UTF-8"?><!-- edited with XMLSpy v2009 (http://www.altova.com) by user (EMBRACE) --><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">    <xs:element name="VMTemplate" type="Template">        <xs:annotation>            <xs:documentation>Virtual Machine Template</xs:documentation>        </xs:annotation>    </xs:element>    <xs:element name="ServerTemplate" type="Template">        <xs:annotation>            <xs:documentation>Physical Machine Template</xs:documentation>        </xs:annotation>    </xs:element>    <xs:complexType name="Template">        <xs:all>            <xs:element name="TemplateID" type="xs:string">                <xs:annotation>                    <xs:documentation>Virtual Machine Template Id (eg.CIDC-T-VM-00000001)                        </xs:documentation>                </xs:annotation>            </xs:element>            <xs:element name="ResourceType" type="xs:string">                <xs:annotation>                    <xs:documentation>Resource Type (eg.CIDC-RT-VM)                        </xs:documentation>                </xs:annotation>            </xs:element>            <xs:element name="MeasureMode" type="xs:string">                <xs:annotation>                    <xs:documentation>Measure Mode (eg.Duration)                        </xs:documentation>                </xs:annotation>            </xs:element>            <xs:element name="TemplateDesc" type="xs:string">                <xs:annotation>                    <xs:documentation>Virtual Machine Template Description                        </xs:documentation>                </xs:annotation>            </xs:element>            <xs:element name="TemplateStatus" type="xs:string">                <xs:annotation>                    <xs:documentation>                        </xs:documentation>                </xs:annotation>            </xs:element>            <xs:element name="TemplateCreator" type="xs:string">                <xs:annotation>                    <xs:documentation>Template Creator                        </xs:documentation>                </xs:annotation>            </xs:element>            <xs:element name="CreateTime" type="xs:string">                <xs:annotation>                    <xs:documentation>Create Time(eg.2011-05-24 08:55:30)                        </xs:documentation>                </xs:annotation>            </xs:element>            <xs:element name="ResourceInfo" type="xs:string">                <xs:annotation>                    <xs:documentation>Resource Information                        </xs:documentation>                </xs:annotation>            </xs:element>        </xs:all>    </xs:complexType></xs:schema>

准备好XML Schema,生成 JAXB 类就很简单了。输入以下命令

xjc -p hello hello.xsd  

一定要在和 Schema的 XX.xsd文件相同的目录中执行上述命令这时会在hello目录下生成ObjectFactory.java , Template.java 两个文件

主要的工作还是用来通过java类来生成相应的xml文件:

测试方法:

public static void main(String[] args) throws Exception {        JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);        ObjectFactory of = new ObjectFactory();        Templatefoo = new Template();        JAXBElement<Foo> e = of.createRoot(foo);        //用于输出元素        Marshaller marshaller = context.createMarshaller();        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);        FileOutputStream stream = new FileOutputStream(new File(args[0]));        marshaller.marshal(e, stream);    }
相关栏目:

用户点评