编程教程
您现在的位置: 中国个人站长站 >> 网络编程 >> Javascript >> 教程正文 从XML到Java代码的数据绑定之二
推荐位

从XML到Java代码的数据绑定之二

中国个人站长站 Javascript 点击数: 更新时间:2007-7-27 14:51:17
称。但是,由于每个接口可能有多个属性,因此该值是另一个具有属性及其类型的 HashMap。最后,必须存储 XML schema 的名称空间,因为 JDOM 将使用这个名称空间来访问 XML schema 中的结构。所有这些具体信息都足以初步勾画出新类的框架,新类在清单 2 中。

  还请注意在清单 2 中已添加了两个需要使用的基本方法:其中一个方法需要使用 XML schema 的 URL 来执行生成(允许它在网络可访问 schema 以及本地 schema 下运行),另一个方法将类输出到指定的目录中。最后,简单的 main 方法将 XML schema 看作一个变量,然后执行生成。

  清单 2. SchemaMapper 类的框架 package org.enhydra.xml.binding;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.List;

// JDOM classes used for document representation
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.NoSuchAttributeException;
import org.jdom.NoSuchChildException;
import org.jdom.input.SAXBuilder;

/**
* <p>
* <code>SchemaMapper</code> handles generation of Java interfaces and classes
* from an XML schema, essentially allowing data contracts to be set up
* for the binding of XML instance documents to Java objects.
* </p>
*
* @author Brett McLaughlin
*/
public class SchemaMapper {

/** Storage for code for interfaces */
private Map interfaces;

/** Storage for code for implementations */
private Map implementations;

/** Properties that accessor/mutators should be created for */
protected Map properties;

/** XML Schema Namespace */
private Namespace schemaNamespace;

/** XML Schema Namespace URI */
private static final String SCHEMA_NAMESPACE_URI =
"http://www.w3.org/1999/xmlSchema";

/**
* <p>
* Allocate storage and set up defaults.
* </p>
*/
public SchemaMapper() {
interfaces = new HashMap();
implementations = new HashMap();
properties = new HashMap();
schemaNamespace = Namespace.getNamespace(SCHEMA_NAMESPACE_URI);
}

/**
* <p>
* This is the "entry point" for generation of Java classes from an XML
* Schema. It allows a schema to be supplied, via <code>URL</code>,
* and that schema is used for input to generation.
* </p>
*
* @param schemaURL <code>URL</code> at which XML Schema is located.
* @throws <code>IOException</code> - when problems in generation occur.
*/
public void generateClasses(URL schemaURL) throws IOException {
// Perform generation
}

/**
* <p>
* This will write out the generated classes to the supplied stream.
* </p>
*
* @param directory <code>File</code> to write to (should be a directory).
* @throws <code>IOException</code> - when output errors occur.
*/
public void writeClasses(File dir) throws IOException {
// Perform output to files
}

/**
* <p>
* This provides a static

上一页  [1] [2] [3] [4] [5] [6] 下一页

教程录入:swh    责任编辑:swh 
个人站长站与你风雨同舟!
本站所提供的资源均来源于互联网,如有侵权行为,请与本站管理员联系,我们会第一时间删除!
·如果您发现《从XML到Java代码的数据绑定之二》文章有错误,也请通知我们修改!
联系邮箱chinageren#126.com,谢谢支持!
站内搜索:
版权所有:中国个人站长站 2007-2008 未经授权禁止复制或建立镜像 客服QQ号:112731235
copyright © 2007-2008 www.ChinaGeRen.com online services. all rights reserved. 苏ICP备05000059号