8

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)

Community
  • 1
  • 1
andbi
  • 4,426
  • 5
  • 45
  • 70

5 Answers5

6

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

madx
  • 6,723
  • 4
  • 55
  • 59
andbi
  • 4,426
  • 5
  • 45
  • 70
  • I had an exception during parseSchema: `java.lang.IllegalArgumentException: system ID 'C:\Users\Federico\Documents\NetBeansProjects\Wadl2Java\myxsd.xsd' isn't absolute` Can you help please? – Federico Bellucci Jan 13 '12 at 17:04
  • 3
    For local files I solved using absolute paths and setting the system id as `is.setSystemId(schemaFile.toURI().toString())` – Federico Bellucci Jan 17 '12 at 10:27
  • @Uccio, sorry, was on vacation, glad you've solved the issue. – andbi Jan 17 '12 at 17:28
3

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));

    }
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
1

The following may help:

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thanks! Do I need extra packages to include? I have neither of com.sun.tools.xjc.* com.sun.tools.xjc.api.* in my JDK 6 classpath. java -version: Java(TM) SE Runtime Environment (build 1.6.0_22-b04) – andbi Nov 22 '10 at 17:46
  • 4.2.Deprecation of any Tool-specific or com.sun.* APIs (JDK 6 Adoption guide). http://www.oracle.com/technetwork/java/javase/adoptionguide-137484.html#4.2 – andbi Nov 22 '10 at 18:01
1

Get the JAXB reference implementation here.

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

0

Another way of getting the dependencies in Maven;

    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-xjc</artifactId>
        <version>2.2.11</version>
    </dependency>
JFK
  • 1,527
  • 16
  • 21