Java Language XJC Generating Java code from simple XSD file


XSD schema (schema.xsd)

The following xml schema (xsd) defines a list of users with attributes name and reputation.

<?xml version="1.0"?>

<xs:schema version="1.0"
    <xs:element name="users" type="ns:Users"/>
    <xs:complexType name="Users">
            <xs:element type="ns:User" name="user" minOccurs="0" maxOccurs="unbounded"/>
    <xs:complexType name="User">
        <xs:attribute name="name" use="required" type="xs:string"/>
        <xs:attribute name="reputation" use="required">
                <xs:restriction base="xs:int">
                    <xs:minInclusive value="1"/>

Using xjc

This requires the path to the xjc tool (JDK binaries) to be in the OS path variable.

The code generation can be started using

xjc schema.xsd

This will generate java files in the working directory.

Result files

There will be some additional comments, but basically the java files generated look like this:

package com.stackoverflow.users;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "Users", propOrder = {
public class Users {

    protected List<User> user;

    public List<User> getUser() {
        if (user == null) {
            user = new ArrayList<User>();
        return this.user;

package com.stackoverflow.users;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "User")
public class User {

    @XmlAttribute(name = "name", required = true)
    protected String name;
    @XmlAttribute(name = "reputation", required = true)
    protected int reputation;

    public String getName() {
        return name;

    public void setName(String value) { = value;

    public int getReputation() {
        return reputation;

    public void setReputation(int value) {
        this.reputation = value;

package com.stackoverflow.users;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;

public class ObjectFactory {

    private final static QName _Users_QNAME = new QName("", "users");

    public ObjectFactory() {

    public Users createUsers() {
        return new Users();

    public User createUser() {
        return new User();

    @XmlElementDecl(namespace = "", name = "users")
    public JAXBElement<Users> createUsers(Users value) {
        return new JAXBElement<Users>(_Users_QNAME, Users.class, null, value);


@javax.xml.bind.annotation.XmlSchema(namespace = "", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.stackoverflow.users;