JAXB Binding Compiler (XJC) that generates Java classes from XML Schema definitions with both command-line and programmatic APIs
npx @tessl/cli install tessl/maven-org-glassfish-jaxb--jaxb-xjc@4.0.0JAXB XJC is a comprehensive code generation tool that automatically transforms XML Schema definitions (XSD) into strongly-typed Java classes, enabling seamless XML data binding within Java applications. As part of the Eclipse Implementation of JAXB (Jakarta XML Binding), this tool provides both command-line and programmatic interfaces for enterprise XML processing workflows.
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>4.0.5</version>
</dependency>import com.sun.tools.xjc.api.XJC;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.Driver;For plugin development:
import com.sun.tools.xjc.Plugin;
import com.sun.tools.xjc.outline.Outline;
import com.sun.tools.xjc.Options;# Basic schema compilation
xjc schema.xsd
# Generate into specific package
xjc -p com.example.generated schema.xsd
# Generate into target directory
xjc -d target/generated-sources schema.xsd
# Enable verbose output
xjc -verbose schema.xsdimport com.sun.tools.xjc.api.XJC;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.ErrorListener;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.writer.FileCodeWriter;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
import java.io.File;
// Create schema compiler
SchemaCompiler compiler = XJC.createSchemaCompiler();
// Configure compiler
compiler.setDefaultPackageName("com.example.generated");
compiler.setErrorListener(new ErrorListener() {
public void error(SAXParseException exception) { /* handle error */ }
public void warning(SAXParseException exception) { /* handle warning */ }
public void info(SAXParseException exception) { /* handle info */ }
});
// Parse schema
compiler.parseSchema(new InputSource("schema.xsd"));
// Compile and generate code
S2JJAXBModel model = compiler.bind();
JCodeModel codeModel = model.generateCode(null, null);
// Write generated classes to filesystem
codeModel.build(new FileCodeWriter(new File("target/generated")));JAXB XJC is built around several key architectural components:
Command-line tool for batch processing XML schemas with comprehensive configuration options. Ideal for build scripts and automated code generation workflows.
public final class Driver {
public static void main(String[] args) throws Exception;
public static int run(String[] args, PrintStream status, PrintStream out) throws Exception;
public static int run(String[] args, XJCListener listener) throws BadCommandLineException;
public static void usage(Options opts, boolean privateUsage);
public static String getBuildID();
}Type-safe Java API for embedding schema compilation in applications, build tools, and IDEs. Provides full control over compilation process and generated code.
public final class XJC {
public static SchemaCompiler createSchemaCompiler();
public static String getDefaultPackageName(String namespaceUri);
}
public interface SchemaCompiler {
ContentHandler getParserHandler(String systemId);
void parseSchema(InputSource source);
void parseSchema(String systemId, Element element);
void parseSchema(String systemId, XMLStreamReader reader);
void setTargetVersion(SpecVersion version);
void setErrorListener(ErrorListener errorListener);
void setEntityResolver(EntityResolver entityResolver);
void setDefaultPackageName(String packageName);
void forcePackageName(String packageName);
void setClassNameAllocator(ClassNameAllocator allocator);
void resetSchema();
S2JJAXBModel bind();
}Extensible plugin architecture for customizing code generation, adding annotations, and implementing domain-specific features.
public abstract class Plugin {
public abstract String getOptionName();
public abstract String getUsage();
public abstract boolean run(Outline outline, Options opt, ErrorHandler errorHandler) throws SAXException;
public int parseArgument(Options opt, String[] args, int i) throws BadCommandLineException, IOException;
public List<String> getCustomizationURIs();
public void onActivated(Options opts) throws BadCommandLineException;
public void postProcessModel(Model model, ErrorHandler errorHandler);
}Bean generation system with customizable field rendering and type mapping for generating optimized JAXB classes.
public interface Outline {
Model getModel();
JCodeModel getCodeModel();
Collection<? extends ClassOutline> getClasses();
ClassOutline getClazz(CClassInfo clazz);
FieldOutline getField(CPropertyInfo fu);
PackageOutline getPackageContext(JPackage _Package);
}
public interface FieldRendererFactory {
FieldRenderer getDefault();
FieldRenderer getArray();
FieldRenderer getList(JClass coreList);
FieldRenderer getSingle();
FieldRenderer getConst(FieldRenderer fallback);
}Comprehensive error reporting system with configurable error listeners and validation support for schema processing and code generation.
public interface ErrorListener {
void error(SAXParseException exception);
void warning(SAXParseException exception);
void info(SAXParseException exception);
}
public abstract class ErrorReceiver {
public abstract void error(SAXParseException exception);
public abstract void warning(SAXParseException exception);
public abstract void info(SAXParseException exception);
public void pollAbort() throws AbortException;
}Ant tasks and build system integration components for Maven, Gradle, and other build tools.
public class XJC2Task extends Task {
// Ant task implementation for XJC
}
public class XJCBase {
// Base class for build tool integration
}public interface S2JJAXBModel extends JAXBModel {
Mapping get(QName elementName);
List<JClass> getAllObjectFactories();
Collection<? extends Mapping> getMappings();
TypeAndAnnotation getJavaType(QName xmlTypeName);
JCodeModel generateCode(Plugin[] extensions, ErrorListener errorListener) throws IOException;
}
public interface JAXBModel {
List<String> getClassList();
}
public class Model implements TypeInfoSet<TypeInfo,CClassInfo,CElementInfo,CEnumLeafInfo> {
public Outline generateCode(Options opt, ErrorReceiver receiver);
public JCodeModel codeModel;
}public class Options {
public boolean debugMode;
public boolean verbose;
public boolean quiet;
public boolean readOnly;
public String encoding;
public File targetDir;
public String defaultPackage;
public int parseArgument(String[] args, int i) throws BadCommandLineException;
public void parseArguments(String[] args) throws BadCommandLineException;
public FieldRendererFactory getFieldRendererFactory();
public void setFieldRendererFactory(FieldRendererFactory frf, Plugin owner);
public ClassLoader getUserClassLoader(ClassLoader parent);
}public abstract class ClassOutline {
public final CClassInfo target;
public final JDefinedClass ref;
public abstract Outline parent();
}
public abstract class FieldOutline {
// Field-specific outline information
}
public interface PackageOutline {
// Package-specific outline information
}public interface Property {
String name();
JType type();
QName elementName();
QName rawName();
}
public interface ClassNameAllocator {
String assignClassName(String packageName, String className);
}
public interface TypeAndAnnotation {
// Type and annotation information
}
public interface Mapping {
// XML to Java mapping information
}public enum SpecVersion {
V2_3("2.3"),
V3_0("3.0");
public final String version;
SpecVersion(String version);
public static SpecVersion parse(String token);
public boolean isLaterThan(SpecVersion other);
}
public enum Language {
DTD("DTD", "dtd"),
XMLSCHEMA("XML Schema", "xsd"),
RELAXNG("RELAX NG", "rng"),
RELAXNG_COMPACT("RELAX NG (compact syntax)", "rnc"),
WSDL("WSDL", "wsdl");
public final String displayName;
public final String[] expectedExtensions;
Language(String displayName, String... expectedExtensions);
}public class BadCommandLineException extends Exception {
public BadCommandLineException(String message);
public BadCommandLineException(String message, Throwable cause);
}
public class AbortException extends RuntimeException {
public AbortException();
public AbortException(String message);
}