扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
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 entry point for class generation from * XML Schemas. * </p> * * @param args <code>String[]</code> list of files to parse. */ public static void main(String[] args) { SchemaMapper mapper = new SchemaMapper(); try { for (int i=0; i<args.length; i++) { File file = new File(args[i]); mapper.generateClasses(file.toURL()); mapper.writeClasses(new File(".")); } } catch (FileNotFoundException e) { System.out.println("Could not locate XML Schema: "); e.printStackTrace(); } catch (IOException e) { System.out.println("Java class generation failed: "); e.printStackTrace(); } } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者