动态生成java源(不带xjc)

发布于 2024-10-03 10:25:35 字数 335 浏览 1 评论 0原文

有没有人设法在没有 XJC 的情况下从 JAXB 模式文件生成 java 代码?

有点类似于

JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler()

用于动态编译java代码。

注意:在 JDK 6 上运行,这意味着 com.sun.* 工具包已被弃用(感谢 Blaise Doughan 获取提示)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

开始看清了 2024-10-10 10:25:35

我必须包含一些 J2EE 库才能使我的解决方案正常工作,因为独立的 JDK 6 无法访问 xjc 实用程序类:

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 源将放置在 outputDirectory

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

那请放手 2024-10-10 10:25:35

此代码在特定目录/包结构中生成文件:

import java.io.File;
import java.io.IOException;

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 JAXCodeGen {
    public static void main(String[] args) throws IOException {

            String outputDirectory = "E:/HEAD/JAXB/src/";

            // Setup schema compiler
            SchemaCompiler sc = XJC.createSchemaCompiler();
            sc.forcePackageName("com.xyz.schema");

            // Setup SAX InputSource
            File schemaFile = new File("Item.xsd");
            InputSource is = new InputSource(schemaFile.toURI().toString());
          //  is.setSystemId(schemaFile.getAbsolutePath());

            // Parse & build
            sc.parseSchema(is);
            S2JJAXBModel model = sc.bind();
            JCodeModel jCodeModel = model.generateCode(null, null);
            jCodeModel.build(new File(outputDirectory));

    }
}

This code generates files at specific Directories/Package structure:

import java.io.File;
import java.io.IOException;

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 JAXCodeGen {
    public static void main(String[] args) throws IOException {

            String outputDirectory = "E:/HEAD/JAXB/src/";

            // Setup schema compiler
            SchemaCompiler sc = XJC.createSchemaCompiler();
            sc.forcePackageName("com.xyz.schema");

            // Setup SAX InputSource
            File schemaFile = new File("Item.xsd");
            InputSource is = new InputSource(schemaFile.toURI().toString());
          //  is.setSystemId(schemaFile.getAbsolutePath());

            // Parse & build
            sc.parseSchema(is);
            S2JJAXBModel model = sc.bind();
            JCodeModel jCodeModel = model.generateCode(null, null);
            jCodeModel.build(new File(outputDirectory));

    }
}
少钕鈤記 2024-10-10 10:25:35

此处获取 JAXB 参考实现。

它包括 com.sun.tools.xjc.api.XJC 类,允许您生成 Java 代码。

Get the JAXB reference implementation here.

It includes the com.sun.tools.xjc.api.XJC class that allows you to generate the Java code.

浅浅 2024-10-10 10:25:35

在 Maven 中获取依赖项的另一种方法;

    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-xjc</artifactId>
        <version>2.2.11</version>
    </dependency>

Another way of getting the dependencies in Maven;

    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-xjc</artifactId>
        <version>2.2.11</version>
    </dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文