-
介质准备
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即可
- Eclipse配置
- 定义系统变量:
- %XMLBEAN_HOME%:XMLBean的安装目录
- %JAVA_HOME%:JDK安装目录
- 建立Java项目
-
由于想使用JDK1.5,所以选择5.0,为了项目管理方便,将原代码与生成的二进制文件分开存放

一定要使用JDK而不是JRE,如果默认是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
运行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.
*/

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();
}
}
本文介绍如何在Eclipse环境中配置XMLBeans并进行XML文件的操作,包括环境搭建、项目配置及示例代码。
233

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



