有没有人设法从没有XJC的JAXB模式文件生成Java代码?
有点类似
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler()
Run Code Online (Sandbox Code Playgroud)
用于动态编译Java代码.
注意:在JDK 6上运行,意味着com.sun.*不推荐使用工具包(感谢Blaise Doughan的提示)
需要以下情况的帮助:用户可以生成自己的数据结构,这些数据结构存储为JAXB-ready XSD源,如下所示:
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Group" type="Group"/>
<xs:element name="Parameter" type="Parameter"/>
<xs:complexType name="Group">
<xs:sequence>
<xs:element name="caption" type="xs:string" minOccurs="0"/>
<xs:element name="parameters" type="Parameter" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Parameter">
<xs:sequence>
<xs:element name="key" type="xs:string" minOccurs="0"/>
<xs:element name="group" type="Group" minOccurs="0"/>
<xs:element name="value" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
在出现新的或修改的模式之后,它将由Schema编译器自动解析,生成,编译和打包到用户jar的java源:
SchemaCompiler sc = XJC.createSchemaCompiler();
// Input source for schema
InputSource is = new InputSource(new StringInputStream(objectPackage.getObjectSchema()));
// Parse
sc.parseSchema(is);
S2JJAXBModel model = sc.bind();
// Generate source
JCodeModel jCodeModel = model.generateCode(null, null);
jCodeModel.build(packageSourceDirectory);
// Compile and package …Run Code Online (Sandbox Code Playgroud)