使用Eclipse开发XMLBeans项目

  1. 介质准备

      Eclipse3.12下载地址:http://www.eclipse.org/downloads/

      XMLBean2.10下载地址:http://xmlbeans.apache.org/sourceAndBinaries/index.html

      JDK1.4+或JDK1.5+下载地址:http://java.sun.com/j2se/1.5.0/download.jsp
    注:如果使用WebLogicServer,可以不用单独安装JDK,使用产品中自带的JDK即可

  1. Eclipse配置
  2.   定义系统变量:
  3.   %XMLBEAN_HOME%:XMLBean的安装目录
  4.   %JAVA_HOME%:JDK安装目录
  5.   建立Java项目建立Java项目
  •   由于想使用JDK1.5,所以选择5.0,为了项目管理方便,将原代码与生成的二进制文件分开存放将原代码与生成的二进制文件分开存放

      一定要使用JDK而不是JRE,如果默认是JRE请重新定义一定要使用JDK而不是JRE

      定义自定义用户类库,并添加所有在%XMLBEAN_HOME%/lib下的Jar包

    定义自定义用户类库

    定义自定义用户类库

    定义自定义用户类库


    把xmlSchema文件加入到工程中,例如加入hello.xsd.
    <? xml version="1.0" encoding="UTF-8" ?>
    <!--
    Copyright 2004 The Apache Software Foundation

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
     
    -->
    < xs:schema  targetNamespace ="http://xmlbeans.apache.org/samples/template/sampletemplate"
        xmlns:xs
    ="http://www.w3.org/2001/XMLSchema"
        elementFormDefault
    ="qualified" >

        
    < xs:element  name ="hello" >
            
    < xs:complexType >
                
    < xs:sequence >
                    
    < xs:element  name ="name"  type ="xs:string"  maxOccurs ="unbounded" />
                
    </ xs:sequence >
            
    </ xs:complexType >
        
    </ xs:element >

    </ xs:schema >

    把ANT文件加入到工程中,例如:
    <!--
    Copyright 2004 The Apache Software Foundation

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
     
    -->
    < project  name ="SampleTemplate"  default ="build" >

        
    < property  environment ="env" />

        
    < path  id ="SampleTemplate.path" >
            
    < path  refid ="xmlbeans.path" />
            
    < fileset  dir ="build/lib"  includes ="*.jar" />
            
    < pathelement  path ="build/classes" />
        
    </ path >

        
    < target  name ="init"  depends  = "clean" >
            
    < property  name ="xmlbeans.home"  value ="${env.XMLBEANS_HOME}" />
            
    < echo  message ="xmlbeans.home: ${xmlbeans.home}" />

            
    <!--  check for xbean.jar from binary distribution  -->
            
    < available
                
    property ="xmlbeans.lib"
                value
    ="${xmlbeans.home}/lib"
                file
    ="${xmlbeans.home}/lib/xbean.jar"   />

            
    <!--  check for xbean.jar compiled from source  -->
            
    < available
                
    property ="xmlbeans.lib"
                value
    ="${xmlbeans.home}/build/lib"
                file
    ="${xmlbeans.home}/build/lib/xbean.jar"   />

            
    < fail  message ="Set XMLBEANS_HOME in your enviornment."
                unless
    ="xmlbeans.lib" />

            
    < echo  message ="xmlbeans.lib: ${xmlbeans.lib}" />
            
    < path  id ="xmlbeans.path" >
                
    < fileset  dir ="${xmlbeans.lib}"  includes ="*.jar" />
            
    </ path >

            
    < taskdef  name ="xmlbean"
                classname
    ="org.apache.xmlbeans.impl.tool.XMLBean"
                classpathref
    ="xmlbeans.path" />
        
    </ target >

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

        
    < target  name ="clean" >
            
    < delete  dir ="build" />
        
    </ target >

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

        
    < target  name ="build"  depends ="init,schemas.jar,SampleTemplate.classes" >
        
    </ target >

        
    < target  name ="schemas.check" >
            
    < uptodate  property ="schemas.notRequired"
                targetfile
    ="build/lib/schemas.jar" >
                
    < srcfiles  dir ="schemas"  includes ="**/*.xsd" />
            
    </ uptodate >
        
    </ target >

        
    < target  name ="schemas.jar"  depends ="init,schemas.check"
            unless
    ="schemas.notRequired" >
            
    < mkdir  dir ="build/lib" />

            
    < xmlbean  schema ="schemas"
                destfile
    ="build/lib/schemas.jar"
                srcgendir
    ="build/src"
                classpathref
    ="xmlbeans.path"
                debug
    ="on"
                
    />
        
    </ target >

        
    < target  name ="SampleTemplate.classes"  depends ="init" >
            
    < mkdir  dir ="build/classes" />

            
    < javac  srcdir ="src"
                destdir
    ="build/classes"
                classpathref
    ="SampleTemplate.path"
                debug
    ="on"
                source
    ="1.4"
                
    />
        
    </ target >

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

        
    < target  name ="run"  depends ="init,build" >
            
    < echo  message ="============================== running SampleTemplate" />
            
    < java
                
    classname ="org.apache.xmlbeans.samples.template.SampleTemplate"
                classpathref
    ="SampleTemplate.path"
                fork
    ="true" >
                
    < arg  line ="xml/hello.xml" />
                
    < arg  line ="xml/bad.xml" />
            
    </ java >
        
    </ target >

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

        
    < target  name ="test"  depends ="init,build" >
            
    < echo  message ="============================== testing SampleTemplate" />
            
    < java
                
    classname ="org.apache.xmlbeans.samples.template.SampleTemplateTest"
                classpathref
    ="SampleTemplate.path"
                fork
    ="true" >
                
    < arg  line ="xml/hello.xml" />
                
    < arg  line ="xml/bad.xml" />
            
    </ java >
        
    </ target >

    </ project >

    使用XMLBeans操作XML的实例代码,SampleTemplate.java
    /*   Copyright 2004 The Apache Software Foundation
     *
     *   Licensed under the Apache License, Version 2.0 (the "License");
     *   you may not use this file except in compliance with the License.
     *   You may obtain a copy of the License at
     *
     *       
    http://www.apache.org/licenses/LICENSE-2.0
     *
     *   Unless required by applicable law or agreed to in writing, software
     *   distributed under the License is distributed on an "AS IS" BASIS,
     *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     *   See the License for the specific language governing permissions and
     *  limitations under the License.
     
    */


    package  org.apache.xmlbeans.samples.template;

    import  org.apache.xmlbeans. * ;
    import  org.apache.xmlbeans.samples.template.sampletemplate.HelloDocument;

    import  java.io.File;
    import  java.util.ArrayList;
    import  java.util.Iterator;

    /**
     * This is a template sample.  A nice description would go here.
     
    */

    public   class  SampleTemplate
    {
        
    /**
         * Prints out the names in the xml instance conforming to hello.xsd.
         
    */

        
    public static void main(String[] args)
            
    throws org.apache.xmlbeans.XmlException, java.io.IOException
        
    {
            SampleTemplate sample 
    = new SampleTemplate();
            sample.start(args);
        }


        
    public void start(String[] args)
            
    throws org.apache.xmlbeans.XmlException, java.io.IOException
        
    {
            
    for (int i = 0; i < args.length; i++)
            
    {
                validate(args[i]);
            }

        }


        
    public void validate(String filename)
            
    throws org.apache.xmlbeans.XmlException, java.io.IOException
        
    {
            System.out.println(
    "parsing document: " + filename);
            HelloDocument doc 
    = HelloDocument.Factory.parse(new File(filename));

            ArrayList errors 
    = new ArrayList();
            XmlOptions opts 
    = new XmlOptions();
            opts.setErrorListener(errors);

            
    if (doc.validate(opts))
            
    {
                System.out.println(
    "document is valid.");

                String[] names 
    = doc.getHello().getNameArray();
                
    for (int i = 0; i < names.length; i++)
                
    {
                    System.out.println(
    " hi there, " + names[i] + "!");
                }

            }

            
    else
            
    {
                System.out.println(
    "document is invalid!");

                Iterator iter 
    = errors.iterator();
                
    while (iter.hasNext())
                
    {
                    System.out.println(
    ">> " + iter.next());
                }

            }

            System.out.println();
        }

    }
    运行Ant文件即可。


     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值