XMLBeans官方demo调试注意事项

问题:给出一个xml文件,利用xmlbeans实现与该xml同schema的xml的读写

 

 

1. 环境:

 1.1 eclipse3.6.2

 1.2 jdk 1.6.0.24

 1.3 xmlbeans-2.5.0

 1.4 Altova XMLSpy 企业版(由大胡子汉化制作)版本2006

 

2. 

  2.1 catalog.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="http://xmlbeans.apache.org/samples/catalog">
	<journal>
		<article>
			<title>Java programming for high-performance numerical computing</title>
			<author>J. E. Moreira et.al.</author>
			<a-very-long-description-element>
				<forsample>This is a sample element for journal-1</forsample>
			</a-very-long-description-element>
		</article>
	</journal>
	<journal>
		<article>
			<title>Dynamically Binding EJBs Using the Java Reflection API</title> 
			<author>Douglas A. Clark</author>
			<a-very-long-description-element>
				<forsample>This document can be found at http://www.cuj.com/documents/s=8467/cujjsup2010clark/clark.htm </forsample>
			</a-very-long-description-element>
		</article>
	</journal>

</catalog>

   注意:xmlns="http://xmlbeans.apache.org/samples/catalog" 这个url可以修改,这个会影响后面生成文件的包路径

 

 2.2 获取catalog.xsd

  XMLSpy中操作 ;免费的trang不是很好用(cmd:java -jar trang.jar catalog.xml catalog.xsd)

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--W3C Schema 由 XMLSpy v2006 U 创建 (http://www.altova.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://xmlbeans.apache.org/samples/catalog" targetNamespace="http://xmlbeans.apache.org/samples/catalog" elementFormDefault="qualified">
	<xs:element name="a-very-long-description-element">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="forsample"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="article">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="title"/>
				<xs:element ref="author"/>
				<xs:element ref="a-very-long-description-element"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="author">
		<xs:simpleType>
			<xs:restriction base="xs:string">
				<xs:enumeration value="Douglas A. Clark"/>
				<xs:enumeration value="J. E. Moreira et.al."/>
			</xs:restriction>
		</xs:simpleType>
	</xs:element>
	<xs:element name="catalog">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="journal" maxOccurs="unbounded"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="forsample">
		<xs:simpleType>
			<xs:restriction base="xs:string">
				<xs:enumeration value="This document can be found at http://www.cuj.com/documents/s=8467/cujjsup2010clark/clark.htm "/>
				<xs:enumeration value="This is a sample element for journal-1"/>
			</xs:restriction>
		</xs:simpleType>
	</xs:element>
	<xs:element name="journal">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="article"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="title">
		<xs:simpleType>
			<xs:restriction base="xs:string">
				<xs:enumeration value="Dynamically Binding EJBs Using the Java Reflection API"/>
				<xs:enumeration value="Java programming for high-performance numerical computing"/>
			</xs:restriction>
		</xs:simpleType>
	</xs:element>
</xs:schema>
 

3.由schema生成xml读写模块

  3.1  

XMLBeans官方demo调试注意事项

  注意:运行时,这个工程只需xbean.jar;xmlbeanlib下的文件在生成catalog.jar时用到

   3.2 build.xml

 <!--

 * Comment for build.xml
 *
 * @author <a href="mailto:[email protected]">yan.dev</a>
 * 
 * @blog:<a href="http://yan-dev.iteye.com"><b>yan.dev's Blog</b></a>
 * @version 1.0
 * @time 2011-3-23 下午03:42:31
 *
 -->
<project name="build" default="build">  
    <target name="init">   
        <available
            property="xmlbeans.lib"
            value="../xmlbeanlib"
            file="../xmlbeanlib/xbean.jar" /> 
        <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"  >  
    	<mkdir dir="../build/src"/>
        <xmlbean schema="../schemas"
            destfile="../lib/catalog.jar"
            srcgendir="../build/src"
            classpathref="xmlbeans.path"
            debug="on"
            />  
    	<antcall target="clean"/>
    </target>   
</project>
 

   注意:xmlbean schema="../schemas" 会自动读取所有schema和xsdconfig,若不是用xsdconfig可直接指定schema文件

3.3 catalog.xsdconfig

 

 

<?xml version="1.0" encoding="UTF-8"?>
<xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config">           
  <xb:namespace uri="http://xmlbeans.apache.org/samples/catalog">
    <xb:package>com.yan.dev.xmlbeans</xb:package>    	
  </xb:namespace> 
</xb:config>
 

 

 注意:uri="http://xmlbeans.apache.org/samples/catalog" 包路径 org.apache.xmlbeans.samples.catalog

   通过xsdconfig把package -〉com.yan.dev.rmi.service,uri也可以省略

  3.4 Test.java

 

 

import java.io.File;
import java.io.IOException;

import org.apache.xmlbeans.XmlException;

import com.yan.dev.xmlbeans.CatalogDocument;
import com.yan.dev.xmlbeans.JournalDocument;

/**
 *  Copyright(c) 2010-2011 Yan.Dev. All Rights Reserved.
 */

/**
 * Comment for Test.java
 * 
 * @author <a href="mailto:[email protected]">yan.dev</a>
 * 
 * @blog:<a href="http://yan-dev.iteye.com"><b>yan.dev's Blog</b></a>
 * @version 1.0
 * @time 2011-3-23 下午06:34:05
 */
public class Test {
	public static void main(String[] args) {
		File xmlfile = new File("xml\\catalog.xml");
		try {
			CatalogDocument catdoc = CatalogDocument.Factory.parse(xmlfile);
			JournalDocument.Journal[] journals = catdoc.getCatalog()
					.getJournalArray();
			for (JournalDocument.Journal j : journals)
				System.out.println(j.getArticle());
		} catch (XmlException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
 

 

注意:JournalDocument.Journal[] journals 是数组,受限于 xs:sequence

 

 

 

 

 

你可能感兴趣的:(apache,xml,Blog,J#,performance)