JAXB 教程

Jaxb教程

庆哥儿版权所有 转载请注明出处http://qmylove.spaces.live.com 谢谢!

KeywordsJaxb 教程 xml schema schemagen xjc 入门 基础

JaxbJ2EETurorial中有第16章有专门论述

JaxbEclipse的插件https://jaxb-workshop.dev.java.net/plugins/eclipse/xjc-plugin.html

可以方便的从schema生成java,而不是像下文第三步中那样用命令行。

开发步骤

1. 下载例如 jaxb-2_1_8.ziphttps://jaxb.dev.java.net/2.1.8/ 解压,目录结构如下

  clip_image002

2. 定义xml文件的schema,用于定义xml文件的格式规范。本例为GolfCountryClub.xsd

一个最简单的schema

<?xml version="1.0" encoding="UTF-8"?>

<schema xmlns="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://www.example.org/GolfCountryClub"

xmlns:tns="http://www.example.org/GolfCountryClub">

<element name="GolfCountryClub">

<complexType>

<sequence>

<element name="GolfCourse" type="tns:GolfCourseType"

maxOccurs="unbounded" minOccurs="1">

</element>

</sequence>

</complexType>

</element>

<complexType name="GolfCourseType">

<sequence>

<element name="Name" type="string">

</element>

</sequence>

<attribute name="NumberOfHoles" type="positiveInteger"

fixed="18">

</attribute>

</complexType>

</schema>

3. 运行xjc.bat或者xjc.sh脚本生成在schema中定义的java对象。

例如:解析GolfCountryClub.xsd并使用-p参数来指定包名

clip_image004

注:同样,可以使用schemagen.bat工具来根据生成的Java类生成schema,例如

clip_image006

生成的schema1.xsd内容如下,和我们原schema基本一致。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="GolfCountryClub">

<xs:complexType>

<xs:sequence>

<xs:element name="GolfCourse" type="GolfCourseType" maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>

</xs:element>

<xs:complexType name="GolfCourseType">

<xs:sequence>

<xs:element name="Name" type="xs:string"/>

</xs:sequence>

<xs:attribute name="NumberOfHoles" type="xs:positiveInteger"/>

</xs:complexType>

</xs:schema>

4. 使用javax.xml.bind.JAXB类的marshal静态方法来根据java对象生成xml文件。

新建一个空的Flex项目,将3中生成的类复制到java 源代码目录中。并将解压后lib文件夹下的jar文件复制到WEB-INFO/lib中。结构如图。

clip_image008

我们的目标是生成一个最简单的xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<ns2:GolfCountryClub

xmlns:ns2="http://www.example.org/GolfCountryClub">

<GolfCourse NumberOfHoles="18">

<Name>The best course</Name>

</GolfCourse>

</ns2:GolfCountryClub>

下面我们新建一个类来生成上面的xml文档

package com.ibm.levin;

import java.io.StringWriter;

import java.io.Writer;

import java.math.BigInteger;

import java.util.ArrayList;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.Marshaller;

import com.ibm.levin.*;

public class CreateXML {

public static void main(String[] args) {

ObjectFactory factory = new ObjectFactory();

GolfCountryClub gcc = factory.createGolfCountryClub();

GolfCourseType gctype = factory.createGolfCourseType();

gctype.setName("The best course");

gctype.setNumberOfHoles(BigInteger.valueOf(18));

gcc.golfCourse = new ArrayList();

gcc.golfCourse.add(0, gctype);

System.out.println(marshall(gcc));

}

public static String marshall(Object jaxbObject) {

try {

JAXBContext jc = JAXBContext.newInstance("com.ibm.levin");

Marshaller marshaller = jc.createMarshaller();

Writer outputWriter = new StringWriter();

marshaller.marshal(jaxbObject, outputWriter);

return outputWriter.toString();

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

}

运行后即可输出上述的xml文档。

5. 实现javax.xml.bind.JAXB类的unmarshal静态方法从xml文件装载java对象。

略。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值