[转载]利用 JAXB 通过 XML 模式生成 XML 文档
g XML 开发人员工具包 (XDK) 产品版(下载)中的 JAXB 类生成器是一个用于从 XML 模式生成 Java 类的编译器。(可以使用 Oracle JDeveloper 10g XML 模式编辑器构建 XML 模式。)JAXB 类生成器代替了 Oracle9i XDK 中的模式类生成器,它生成表示 XML 模式中各种元素和 complexType 声明的 Java 类。(用于 XML 绑定的 Java 体系结构,即 JAXB,是一种用于将 XML 模式绑定到 Java 代码表示的技术。)然后,J2EE 开发人员就可以使用 JAXB 类生成器生成的 Java 类来构建符合 XML 模式的 XML 文档了。
g XDK JAXB 类生成器类以及命令行实用程序 orajaxb 必须加入类路径 (Classpath) 中。将 xdk_nt_10_1_0_2_0_production.zip 文件解压缩到安装目录。将 <XDK>/lib/xml.jar、<XDK>/lib/xmlparserv2.jar 和 <XDK>/lib/xmlmesg.jar 添加到 Classpath 变量。<XDK> 是 Oracle 10g XDK 产品版所安装在的目录。
<xs:element> 表示,complexType 则使用 <xs:complexType> 表示。
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://jaxbderived/catalog"xmlns:catalog="http://jaxbderived/catalog" elementFormDefault="qualified"> <xs:element name="OracleCatalog" type="catalog:catalog"/> <xs:complexType name="catalog"> <xs:sequence> <xs:element name="journal" type="catalog:journal" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="title" type="xs:string"/> <xs:attribute name="publisher" type="xs:string"/> </xs:complexType> <xs:complexType name="journal"> <xs:sequence> <xs:element name="article" type="catalog:article" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="date" type="xs:string"/> </xs:complexType> <xs:complexType name="article"> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="section" type="xs:string"/> </xs:complexType> </xs:schema>
>java oracle.xml.jaxb.orajaxb -schema OracleCatalog.xsd -outputDir classes
指定在其中生成 Java 类的目录。 schema <schemaFile> 指定从中生成 Java 类的 XML 模式。 targetPkg <targetPkg> 指定目标程序包名称。 package jaxbderived.catalog;public class ObjectFactory {public jaxbderived.catalog.Journal createJournal() {jaxbderived.catalog.Journal elem = new jaxbderived.catalog.JournalImpl(ownerDocument);return elem; }public jaxbderived.catalog.Article createArticle() {jaxbderived.catalog.Article elem = new jaxbderived.catalog.ArticleImpl(ownerDocument);return elem; }public jaxbderived.catalog.Catalog createCatalog() {jaxbderived.catalog.Catalog elem = new jaxbderived.catalog.CatalogImpl(ownerDocument);return elem; }public jaxbderived.catalog.OracleCatalog createOracleCatalog() {jaxbderived.catalog.OracleCatalog elem = new jaxbderived.catalog.OracleCatalogImpl(ownerDocument);return elem; }public Object newInstance (Class javaContentInterface) throws javax.xml.bind.JAXBException {Object newInstance = null;String elemName = javaContentInterface.getName();try {if (elemName.equals("jaxbderived.catalog.OracleCatalog")) {newInstance = new jaxbderived.catalog.OracleCatalogImpl(ownerDocument);return newInstance; } }catch (Exception e) {throw new javax.xml.bind.JAXBException(e.toString()); }return null; }public Object getProperty(String name) {return null; }public void setProperty(String name, Object value) { }public Object unmarshal(org.w3c.dom.Node node) throws javax.xml.bind.UnmarshalException {String elemName = node.getLocalName();try {if (elemName.equals("OracleCatalog")) {jaxbderived.catalog.OracleCatalog unode = new jaxbderived.catalog.OracleCatalogImpl((oracle.xml.parser.v2.XMLElement)node);return unode; } }catch (Exception e) {throw new javax.xml.bind.UnmarshalException(e.toString()); }return null; }private oracle.xml.parser.v2.XMLDocument ownerDocument = new oracle.xml.parser.v2.XMLDocument();}
OracleCatalog 而生成的 Java 接口。 package jaxbderived.catalog;public interface OracleCatalog extends jaxbderived.catalog.Catalog, javax.xml.bind.Element{}
OracleCatalog 而生成的 Java 类: package jaxbderived.catalog;public class OracleCatalogImpl extends jaxbderived.catalog.CatalogImpl implements jaxbderived.catalog.OracleCatalog{public OracleCatalogImpl(oracle.xml.parser.v2.XMLDocument ownerDoc) {super("OracleCatalog", "http://jaxbderived/catalog", ownerDoc); }public OracleCatalogImpl(String name, String namespace, oracle.xml.parser.v2.XMLDocument ownerDoc) {super(name, namespace, ownerDoc); }public OracleCatalogImpl(oracle.xml.parser.v2.XMLElement node) {super(node); }}
catalog 而生成的 Java 接口。 package jaxbderived.catalog;public interface Catalog{public void setTitle(java.lang.String t);public java.lang.String getTitle();public void setPublisher(java.lang.String p);public java.lang.String getPublisher();public java.util.List getJournal();}
catalog 而生成的 Java 类: package jaxbderived.catalog;public class CatalogImpl extends oracle.xml.jaxb.JaxbNode implements jaxbderived.catalog.Catalog{public CatalogImpl(oracle.xml.parser.v2.XMLDocument ownerDoc) {super("catalog", "http://jaxbderived/catalog", ownerDoc); }public CatalogImpl(String name, String namespace, oracle.xml.parser.v2.XMLDocument ownerDoc) {super(name, namespace, ownerDoc); }public CatalogImpl(oracle.xml.parser.v2.XMLElement node) {super(node); }public void setTitle(java.lang.String t) {super.setJaxbAttrStringValue("title", "", t); }public java.lang.String getTitle() {return super.getJaxbAttrStringValue("title", ""); }public void setPublisher(java.lang.String p) {super.setJaxbAttrStringValue("publisher", "", p); }public java.lang.String getPublisher() {return super.getJaxbAttrStringValue("publisher", ""); }public java.util.List getJournal() {return (java.util.List)super.getList("journal", "http://jaxbderived/catalog", this, 0); }public Object createJaxbNode(oracle.xml.parser.v2.XMLNode node) {String name = node.getLocalName();String namespace = node.getNamespaceURI();if (namespace == null)namespace = "";if (name.equals("journal") && namespace.equals("http://jaxbderived/catalog")) {jaxbderived.catalog.JournalImpl obj = new jaxbderived.catalog.JournalImpl(getOwnerDocument());obj.populateNodeArray(node);return obj; }return null; }public void populateNodeArray(oracle.xml.parser.v2.XMLNode node) {String name, namespace;oracle.xml.parser.v2.XMLNode n = (oracle.xml.parser.v2.XMLNode)node.getFirstChild();while (n != null) {name = n.getLocalName(); namespace = n.getNamespaceURI();if (namespace == null)namespace = "";if (name.equals("journal") && namespace.equals("http://jaxbderived/catalog")) {super.setNodeVectorValue(0, n); }n = (oracle.xml.parser.v2.XMLNode)n.getNextSibling(); }super.populateNodeArray(node); }static final Object[] _Journal = {};static final Object[] _Catalog = {_Journal };protected Object[] getSchemaObject() {return _Catalog; }}
CatalogImpl,并对 CatalogImpl 类对象进行编组,以构建一个 XML 文档。首先,导入 javax.xml.bind 程序包。 import javax.xml.bind.*;
import oracle.xml.jaxb.*;
catalog 对象编组到 XML 文档中。 JaxbContextImpl jaxbContext=new JaxbContextImpl();Marshaller marshaller=jaxbContext.createMarshaller();
ObjectFactory,我们将从中创建一个实现类的新实例。ObjectFactory 是 JAXB 的一个重要特性,因为它提供代码可移植性。 ObjectFactory factory=new ObjectFactory();
catalog 元素: CatalogImpl catalog=(CatalogImpl)(factory.createCatalog());
catalog 元素的 title 属性: catalog.setTitle("Oracle Magazine");
catalog.setPublisher("Oracle Publishing");
journal 元素。 JournalImpl journal=(JournalImpl)(factory.createJournal());
journal 元素的 date 属性。 journal.setDate("November-December 2003");
journal 元素添加到 catalog 元素。 java.util.List journalList=catalog.getJournal();journalList.add(journal);
journal 元素中创建 article 元素。 ArticleImpl article=(ArticleImpl)(factory.createArticle());
article 元素的 section 属性。 article.setSection("XML");
article 元素中创建 title 元素。 article.setTitle("Updating XQuery");
journal 元素添加 article 元素。 java.util.List articleList=journal.getArticle();articleList.add(article);
article 元素中创建 author 元素。 java.util.List authorList=article.getAuthor();authorList.add("Jason Hunter");
journal 元素相似,添加另外一个 journal 元素,以创建示例 XML 文档 OracleCatalog.xml。然后将 CatalogImpl 对象编组到一个 XML 文档。 marshaller.marshal(catalog, new FileOutputStream(xmlDocument));
<?xml version="1.0" encoding = 'UTF-8'?><catalog title="Oracle Magazine" publisher="Oracle Publishing" xmlns="http://jaxbderived/catalog"><journal date="November-December 2003"><article section="XML"><title>Updating XQuery</title><author>Jason Hunter</author></article></journal><journal date="September-October 2003"><article section="SQL"><title>The Active Database</title><author> Cameron ORourke</author></article></journal></catalog>
import jaxbderived.catalog.*;import oracle.xml.jaxb.*;import oracle.xml.parser.v2.*;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import javax.xml.bind.*;public class XMLConstructor { public void generateXMLDocument(File xmlDocument){try {JaxbContextImpl jaxbContext=new JaxbContextImpl();Marshaller marshaller=jaxbContext.createMarshaller(); ObjectFactory factory=new ObjectFactory(); CatalogImpl catalog=(CatalogImpl)(factory.createCatalog());catalog.setTitle("Oracle Magazine");catalog.setPublisher("Oracle Publishing"); JournalImpl journal=(JournalImpl)(factory.createJournal());journal.setDate("November-December 2003"); ArticleImpl article=(ArticleImpl)(factory.createArticle()); article.setSection("XML");article.setTitle("Updating XQuery"); java.util.List journalList=catalog.getJournal();journalList.add(journal);java.util.List articleList=journal.getArticle();articleList.add(article);java.util.List authorList=article.getAuthor();authorList.add("Jason Hunter"); journal=(JournalImpl)(factory.createJournal());journal.setDate("September-October 2003"); article=(ArticleImpl)(factory.createArticle()); article.setSection("SQL");article.setTitle("The Active Database"); journalList=catalog.getJournal();journalList.add(journal);articleList=journal.getArticle();articleList.add(article);authorList=article.getAuthor();authorList.add("Cameron ORourke");marshaller.marshal(catalog, new FileOutputStream(xmlDocument)); }catch (IOException e) {System.out.println(e.toString()); }catch (JAXBException e) {System.out.println(e.toString()); } }public static void main (String[] argv) { String xmlDocument=argv[0];XMLConstructor xmlConstructor=new XMLConstructor();xmlConstructor.generateXMLDocument(new File(xmlDocument)); }}