繁体   English   中英

通过表示XSD模式的字符串以编程方式创建Java类

[英]Programmatically create java classes from a String representing the xsd schema

我设法从xsd文件创建类:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="author" type="author"/>

  <xs:element name="book" type="book"/>

  <xs:complexType name="author">
    <xs:sequence>
      <xs:element name="firstName" type="xs:string" minOccurs="0"/>
      <xs:element name="lastName" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="book">
    <xs:sequence>
      <xs:element ref="author" minOccurs="0"/>
      <xs:element name="pages" type="xs:int"/>
      <xs:element name="publicationDate" type="xs:dateTime" minOccurs="0"/>
      <xs:element name="title" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

但我无法从表示相同xsd的String生成相同的类。

我在下面粘贴了我的代码:

  • main_working从文件生成类并正常工作
  • main_not_working会引发异常。

这是代码:

import java.io.File;
import java.io.StringReader;

import org.xml.sax.InputSource;

import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;

public class XsdMain {

    private static File out = new File("out");
    private static String xsd = 
            "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
            "<xs:schema version=\"1.0\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">" +
                "<xs:element name=\"author\" type=\"author\"/>" +
                "<xs:element name=\"book\" type=\"book\"/>" +
                "<xs:complexType name=\"author\">" +
                    "<xs:sequence>" +
                        "<xs:element name=\"firstName\" type=\"xs:string\" minOccurs=\"0\"/><xs:element name=\"lastName\" type=\"xs:string\" minOccurs=\"0\"/>" +
                    "</xs:sequence>" +
                "</xs:complexType>" +
                "<xs:complexType name=\"book\">" +
                    "<xs:sequence>" +
                        "<xs:element ref=\"author\" minOccurs=\"0\"/>" +
                        "<xs:element name=\"pages\" type=\"xs:int\"/>" +
                        "<xs:element name=\"publicationDate\" type=\"xs:dateTime\" minOccurs=\"0\"/>" +
                        "<xs:element name=\"title\" type=\"xs:string\" minOccurs=\"0\"/>" +
                    "</xs:sequence>" +
                "</xs:complexType>" +
            "</xs:schema>";

    public static void main(String[] args) throws Exception {
        main_working();       // this works
        main_not_working();   // this doesn't work
    }

    public static void main_working() throws Exception {
        // Setup schema compiler
        SchemaCompiler sc = XJC.createSchemaCompiler();
        sc.forcePackageName("com.xyz.schema");

        // Setup SAX InputSource
        File schemaFile = new File("in/test.xsd");
        InputSource is = new InputSource(schemaFile.toURI().toString());

        // Parse & build
        sc.parseSchema(is);
        S2JJAXBModel model = sc.bind();
        JCodeModel jCodeModel = model.generateCode(null, null);
        jCodeModel.build(out);

    }

    public static void main_not_working() throws Exception {
        // Setup schema compiler
        SchemaCompiler sc = XJC.createSchemaCompiler();
        sc.forcePackageName("com.xyz.schema");

        // Setup SAX InputSource
        InputSource is = new InputSource( new StringReader( xsd ) );

        // Parse & build
        sc.parseSchema(is);
        S2JJAXBModel model = sc.bind();
        JCodeModel jCodeModel = model.generateCode(null, null);
        jCodeModel.build(out);

    }
}

抛出的异常是这个:

Exception in thread "main" java.lang.NullPointerException
    at java.net.URI$Parser.parse(URI.java:3035)
    at java.net.URI.<init>(URI.java:607)
    at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.checkAbsoluteness(SchemaCompilerImpl.java:163)
    at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.parseSchema(SchemaCompilerImpl.java:130)
    at com.madx.XsdMain.main_not_working(XsdMain.java:71)
    at com.madx.XsdMain.main(XsdMain.java:42)

参见http://docs.oracle.com/javase/7/docs/api/org/xml/sax/InputSource.html#setCharacterStream(java.io.Reader)

应用程序编写者应使用setSystemId()提供解析相对URI的基础,并可以使用setPublicId包含公共标识符。

您可能会得到一个nullpointer,因为未设置SystemId。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM