java xjc,动态生成java源码(没有xjc)

Has anyone managed to generate java code from a JAXB schema file without XJC?

Somewhat similar to

JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler()

used to dynamically compile java code on the fly.

Note: Running on JDK 6, meaning that com.sun.* tools packages are deprecated (thanks Blaise Doughan for the hint)

解决方案

I had to include some J2EE libraries for my solution to work cause standalone JDK 6 provides no access to xjc utility classes:

import com.sun.codemodel.*;

import com.sun.tools.xjc.api.*;

import org.xml.sax.InputSource;

// Configure sources & output

String schemaPath = "path/to/schema.xsd";

String outputDirectory = "schema/output/source/";

// Setup schema compiler

SchemaCompiler sc = XJC.createSchemaCompiler();

sc.forcePackageName("com.xyz.schema.generated");

// Setup SAX InputSource

File schemaFile = new File(schemaPath);

InputSource is = new InputSource(new FileInputStream(schemaFile));

is.setSystemId(schemaFile.getAbsolutePath());

// Parse & build

sc.parseSchema(is);

S2JJAXBModel model = sc.bind();

JCodeModel jCodeModel = model.generateCode(null, null);

jCodeModel.build(new File(outputDirectory));

*.java sources will be placed in outputDirectory

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值