以编程方式从表示xsd架构的String创建java类

时间:2016-07-18 10:06:45

标签: 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)

1 个答案:

答案 0 :(得分:2)

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

  

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

你可能得到一个nullpointer,因为没有设置SystemId。