1、Introduction to XMLBeans
XMLBeans is a tool that allows you to access the full power of XML in a Java friendly way. It is an XML-Java binding tool. The idea is that you can take advantage the richness and features of XML and XML Schema and have these features mapped as naturally as possible to the equivalent Java language and typing constructs. XMLBeans uses XML Schema to compile Java interfaces and classes that you can then use to access and modify XML instance data. Using XMLBeans is similar to using any other Java interface/class, you will see things like getFoo or setFoo just as you would expect when working with Java. While a major use of XMLBeans is to access your XML instance data with strongly typed Java classes there are also API's that allow you access to the full XML infoset (XMLBeans keeps full XML Infoset fidelity) as well as to allow you to reflect into the XML schema itself through an XML Schema Object model.
译:XMLBeans是一个工具,通过它可以在Java中访问XML的强大功能。它是将XML和Java绑定的工具。这意味着,你能享受XML和XML Schema的超级特性,并且可以将其很好地转换为Java代码。XMLBeans利用XML Schema编译成Java接口和类,以至于你能用来访问和修改实例化的XML数据。使用XMLBeans与使用Java接口/类一样,你将看到象SET和GET这样的方法,它们是在Java中经常用到的。XMLBeans的主要用处是通过强大的Java类来访问实例化XML数据,利用Java类中的API可以访问XML中的所有元素(XMLBeans很好的保存着这些元素),也允许你通过XML Schema对象模型展现XML Schema本身。
2、编写XML Schema
例如:〔Template.xsd〕
<?xml version="1.0" encoding="GB2312"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://cms.creator/template/data" xmlns:data="http://cms.creator/template/data" elementFormDefault="qualified">
<xsd:complexType name="template">
<xsd:sequence>
<xsd:element name="template" type="data:template" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="ID" type="xsd:string"/>
<xsd:attribute name="CHINA_NAME" type="xsd:string"/>
<xsd:attribute name="MAIN_FILE_NAME" type="xsd:string"/>
<xsd:attribute name="ROOT_PATH" type="xsd:string"/>
<xsd:attribute name="MEMO" type="xsd:string"/>
<xsd:attribute name="CREATE_TIME" type="xsd:string"/>
<xsd:attribute name="CREATE_AUTHOR" type="xsd:string"/>
<xsd:attribute name="IS_OPEN" type="xsd:string"/>
<xsd:attribute name="PARENT_ID" type="xsd:string"/>
<xsd:attribute name="IS_DIRECTORY" type="xsd:string"/>
</xsd:complexType>
<xsd:element name="template" type="data:template"/>
</xsd:schema>
3、编译Schema
注:本来要求自己搭环境编译的,但Apache提供了网上直接编译的窗口,下次再写XMLBeans环境如何搭建的吧!
(1)输入:http://xmlbeans.webappshosting.com/schemaToolsV2/compile.do(注意:XMLBeans的版本为2.0)
(2)选择多选框:Keep XMLBeans Java Source(可以查看源代码)和Enable Downloads(可以下载Jar包),输入Jar包的名称(如:SchemaOfTemplate.jar)
(3)下载Jar包(注意:包不可以直接使用,要重新打,因为包中没有META-INF目录)
(4)将生成creator.cms.template.data.TemplateDocument和creator.cms.template.data.Template两个主类和一些附属类
4、下载xmlbeans-2.0.zip(http://xmlbeans.apache.org),使用xbean.jar和jsr173_api.jar包
5、这样就可以使用了,好像对Template数据结构对象一样操作,如SET和GET方法,太棒了!
import creator.cms.template.data.*;
import creator.cms.template.data.TemplateDocument.*;
TemplateDocument templateDocument = TemplateDocument.Factory.newInstance();
Template template = templateDocument.addNewTemplate();
template.setID(ID);
template.setCHINANAME(CHINA_NAME);
template.setMAINFILENAME(MAIN_FILE_NAME);
template.setROOTPATH(ROOT_PATH);
template.setMEMO(MEMO);
template.setCREATETIME(CREATE_TIME);
template.setCREATEAUTHOR(CREATE_AUTHOR);
template.setISOPEN(IS_OPEN);
template.setPARENTID(PARENT_ID);
template.setISDIRECTORY(IS_DIRECTORY);
templateDocument.toString()
<template ID="1" CHINA_NAME="模板五" MAIN_FILE_NAME="indexw.html" ROOT_PATH="E:/MyProjects/R_MHWZ/MHWZ/cms/template/indexw" MEMO="模板五:说明" CREATE_TIME="2005-03-02 16:42:47" CREATE_AUTHOR="谭波" IS_OPEN="0" PARENT_ID="0" IS_DIRECTORY="1" xmlns="http://cms.creator/template/data">
<template ID="8300" CHINA_NAME="模板一" MAIN_FILE_NAME="c.htm" ROOT_PATH="E:/MyProjects/R_MHWZ/MHWZ/cms/template/c" MEMO="模板一:说明" CREATE_TIME="2005-03-02 16:34:12" CREATE_AUTHOR="谭波" IS_OPEN="0" PARENT_ID="1" IS_DIRECTORY="1"/>
<template ID="8400" CHINA_NAME="模板二" MAIN_FILE_NAME="fxrs.htm" ROOT_PATH="E:/MyProjects/R_MHWZ/MHWZ/cms/template/fxrs" MEMO="模板二:说明" CREATE_TIME="2005-03-02 16:35:14" CREATE_AUTHOR="谭波" IS_OPEN="0" PARENT_ID="1" IS_DIRECTORY="1"/>
<template ID="8500" CHINA_NAME="模板三" MAIN_FILE_NAME="goodnews.htm" ROOT_PATH="E:/MyProjects/R_MHWZ/MHWZ/cms/template/goodnews" MEMO="模板三:说明" CREATE_TIME="2005-03-02 16:35:31" CREATE_AUTHOR="谭波" IS_OPEN="0" PARENT_ID="1" IS_DIRECTORY="0">
<template ID="8600" CHINA_NAME="模板四" MAIN_FILE_NAME="index.htm" ROOT_PATH="E:/MyProjects/R_MHWZ/MHWZ/cms/template/index" MEMO="模板四:说明" CREATE_TIME="2005-03-02 16:35:49" CREATE_AUTHOR="谭波" IS_OPEN="0" PARENT_ID="8500" IS_DIRECTORY="1"/>
</template>
</template>
XMLBeans是一个XML-Java绑定工具,它根据XML Schema编译Java接口和类,使得开发者能以类型安全的方式访问和修改XML数据。通过XMLBeans,可以像操作普通Java对象一样操作XML,提供了类似于getFoo和setFoo的方法。本文介绍了如何使用XMLBeans,从编写XML Schema,到在线编译,下载并使用生成的Java类进行XML实例数据的操作。
590

被折叠的 条评论
为什么被折叠?



