使用XMLBeans 处理XML

 

.XMLBeans

基于XML开发的技术很多,常见的如Dom4j(JBoss),Castor,Common-Degister(Struts),XMLBeans.关于这些的比较网上很多.这里谈谈XMLBeans的用法.

XMLBeans的官方地址(http://xmlbeans.apache.org/).上面有它的详细介绍和用法.

 

适用的场合

从使用的经验来看,XMLBeans比较适合处理具有复杂Schema定义的xml,SAML,Liberty.而简单的XML结构用Dom4j或者Castor XML Mapping 都很容易处理.

因此如果你要处理一个很复杂的Schema定义,选择XMLbeans就比较好.

 

. XMLBeans 的使用过程

1.要熟悉XML Schema. 这本书值得推荐: XML 模式权威教程 http://www.china-pub.com/computers/common/info.asp?id=8908)

 

2.Schema 自动映射到 Java 对象.

这个步骤可以用XMLbeans的生成工具.

相关的Maven脚本如下:

         <!--==================================================================-->

<!-- XMLBeans                                                         -->

<!--==================================================================-->

<goal description="xmlbeans generate " name="project:xmlbeans-gen" >

<echo>+---------------------------------------------------+</echo>

<echo>| XMLBeans genreating....... |                      </echo>

<echo>+---------------------------------------------------+</echo>

<taskdef name="xmlbean"

classname="org.apache.xmlbeans.impl.tool.XMLBean"

classpathref="maven.dependency.classpath"/>

<xmlbean srcgendir="${project.xmlbeans.srcgendir}"

classgendir="${project.xmlbeans.classgendir}"

destfile="${project.xmlbeans.destfile}" debug="true"

classpathref="maven.dependency.classpath" >

<fileset dir="${project.xmlbeans.schemas}" />

</xmlbean>

<!--

<path id="{maoxiang.xmlbeans.srcgendir" location="${maoxiang.xmlbeans.srcgendir}"/>

<maven:addPath id="maven.compile.src.set" refid="maoxiang.xmlbeans.srcgendir"/>

-->

</goal>

 

这个脚本调用XMLBeansant脚本(xmlbeans文档),生成了一个可以处理Schema的框架.

 

3. 使用XMLBeans 解析 xml

根据Schema的生成框架,就可以处理符合该Schema定义的XML实例了.这点和Eclipse下的EMF框架十分类似.

XMLBeans框架的两个最重要的方法:

a) Factory 用来解析XML, PortletAppType portletApp = PortletAppType.Factory.parse(file); 需要提醒的是,
  one: parse
的参数虽然可以是各种类型,不过最好是 Stream. 如果直接是File,web环境下,会报错为 Content is not allowed  in prolog.

  two: 在Jboss下使用时,不需要使用jaxen这个包.
b) XmlText()
可以将对象的数据导出为xml格式,而且不一定是要根元素.

看看下面的代码片断:

  portlet.xml中读取启动参数.

       public static Properties getInitParamProps(PortletType portlet) {

Properties props = new Properties();

 

InitParamType[] inits = portlet.getInitParamArray();

 

for (int i = 0; i < inits.length; i++) {

props.setProperty(inits[i].getName().getStringValue(), inits[i]

.getValue().getStringValue());

}

return props;

}

 

 

4.总结

使用XMLBeans处理XML,简单.高效(可以看看xmlbeans架构师的blog:http://davidbau.com/).比较难的在于如果要处理简单的xml结构,你还得定义一个schema方便自动产生xmlbeans框架. Schema本身就是一种语言,建议采用 XML Spy(home edition 是免费的) 来编写Schema.

下面这个Schema就是我的Portal LayoutSchema,XMLBeans来处理的效果很好.

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

<xsd:schema xmlns:portal="http://maoxiang/portal" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://maoxiang/portal" elementFormDefault="unqualified">

<xsd:complexType name="baseType" abstract="true">

<xsd:sequence>

<xsd:element name="preference" type="portal:preferenceType" minOccurs="0" maxOccurs="unbounded">

<xsd:unique name="name-uniqueness">

<xsd:selector xpath="./preference"/>

<xsd:field xpath="name"/>

</xsd:unique>

</xsd:element>

</xsd:sequence>

</xsd:complexType>

<xsd:complexType name="preferenceType">

<xsd:sequence>

<xsd:element name="name" type="xsd:string"/>

<xsd:element name="value" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>

</xsd:sequence>

</xsd:complexType>

<!-- define the page layout -->

<xsd:element name="page" type="portal:pageType" />

<xsd:complexType name="pageType">

<xsd:complexContent>

<xsd:extension base="portal:baseType">

<xsd:sequence>

<xsd:element name="create-time" type="xsd:dateTime"/>

<xsd:element name="page-title" type="xsd:string"/>

<xsd:element name="page-width" type="xsd:string"/>

<xsd:element name="page-language" type="xsd:language"/>

<xsd:element name="page-refresh" type="xsd:integer"/>

<xsd:element name="page-style" type="portal:styleType"/>

<xsd:element name="page-layout" type="portal:layoutType"/>

</xsd:sequence>

</xsd:extension>

</xsd:complexContent>

</xsd:complexType>

<xsd:complexType name="styleType">

<xsd:complexContent>

<xsd:extension base="portal:baseType">

<xsd:sequence>

<xsd:element name="css" type="xsd:string"/>

</xsd:sequence>

</xsd:extension>

</xsd:complexContent>

</xsd:complexType>

<xsd:complexType name="layoutType">

<xsd:complexContent>

<xsd:extension base="portal:baseType">

<xsd:sequence>

<xsd:element name="row" type="portal:rowType" minOccurs="0" maxOccurs="unbounded">

<xsd:unique name="row_uuid">

<xsd:selector xpath="."/>

<xsd:field xpath="@id"/>

</xsd:unique>

</xsd:element>

</xsd:sequence>

</xsd:extension>

</xsd:complexContent>

</xsd:complexType>

<xsd:complexType name="rowType">

<xsd:complexContent>

<xsd:extension base="portal:baseType">

<xsd:sequence>

<xsd:element name="col" type="portal:colType" minOccurs="0" maxOccurs="unbounded">

<xsd:unique name="col_uuid">

<xsd:selector xpath="."/>

<xsd:field xpath="@id"/>

</xsd:unique>

</xsd:element>

</xsd:sequence>

<xsd:attribute name="id" type="xsd:string" use="required"/>

</xsd:extension>

</xsd:complexContent>

</xsd:complexType>

<xsd:complexType name="colType">

<xsd:complexContent>

<xsd:extension base="portal:baseType">

<xsd:sequence>

<xsd:element name="unit" type="portal:unitType" minOccurs="0" maxOccurs="unbounded">

<xsd:unique name="unit_uuid">

<xsd:selector xpath="."/>

<xsd:field xpath="@id"/>

</xsd:unique>

</xsd:element>

</xsd:sequence>

<xsd:attribute name="id" type="xsd:string" use="required"/>

<xsd:attribute name="width" type="xsd:string" use="required"/>

</xsd:extension>

</xsd:complexContent>

</xsd:complexType>

<xsd:complexType name="unitType">

<xsd:complexContent>

<xsd:extension base="portal:baseType">

<xsd:sequence>

<xsd:element name="portlet-id" type="xsd:string"/>

</xsd:sequence>

<xsd:attribute name="id" type="xsd:string" use="required"/>

</xsd:extension>

</xsd:complexContent>

</xsd:complexType>

</xsd:schema>

 

 

 

你可能感兴趣的:(xml,properties,schema,String,extension,portlet)