java - 以编程方式从表示 xsd 架构的字符串创建 java 类

标签 java xml xsd code-generation

我设法从 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 的字符串生成相同的类。

我将代码粘贴在下面:

  • 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)

Application writers should use setSystemId() to provide a base for resolving relative URIs, and may use setPublicId to include a public identifier.

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

关于java - 以编程方式从表示 xsd 架构的字符串创建 java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38433942/

相关文章:

java - Python Java 套接字问题

java - Spring REST 从路由中获取 Controller +方法?

PHP Web 服务客户端 : Guzzle and xsd

java.net.ConnectException : Validating Xml against XSD : local machine

java - 使用 KeyStore.getEntry() 时出现 UnsupportedOperationException?

java - 尝试使用 Java 从 AWS Gateway 获取 HTTP header 等

java - XPath 从多个节点获取文本

python - 使用 ElementTree 解析 XML 文件的一部分时遇到困难

xml - 如何从 XML 样本生成 XSLT

java - 具有列表外部重复属性的复杂类型的 XSD 声明