1

我正在编写一个类来在 java 中运行 xjc。我的代码如下:

SchemaCompiler sc = XJC.createSchemaCompiler();
 URL url = new URL("file://E:\\JAXB\\books.xsd");
 sc.parseSchema(new InputSource(url.toExternalForm()));
 S2JJAXBModel model = sc.bind();
 JCodeModel cm = model.generateCode(null, null);
 cm.build(new FileCodeWriter(new File("E:\\JAXBTest")));

当我运行它时,我得到模型为空。

任何人都可以帮助我或提供任何我可以知道的链接。

4

1 回答 1

2

如果您查看SchemaCompilerAPI 中的bind()方法,它会说:

如果编译失败,bind() 返回 null。在这种情况下,错误应该已经传递给注册的错误处理程序。

因此,您需要使用以下内容注册错误侦听器SchemaCompiler.setErrorListener()

sc.setErrorListener(new ErrorListener(){
  public void error(SAXParseException exception){
    exception.printStackTrace();
  }
});

并希望您能获得更多关于出了什么问题的信息。

于 2010-12-30T12:57:44.053 回答