xjc 是 Java 中的 XML 数据绑定编译器,用于根据 XML 模式(XMLSchema)文件生成对应的 Java 类。
xjc [-options ...] <schema file/URL/dir/jar> ... [-b <bindinfo>] ...
注意:
(1)如果指定 dir, 将编译该目录中的所有模式文件。
(2)如果指定 jar, 将编译 /META-INF/sun-jaxb.episode 绑定文件。
命令支持如下选项:
-nv 不对输入模式执行严格验证
-extension 允许供应商扩展 - 不严格遵循 JAXB 规范中的兼容性规则和应用程序 E.2
-b <file/dir> 指定外部绑定文件 (每个 <file> 必须具有自己的 -b) 如果指定目录, 则将搜索 **/*.xjb
-d <dir> 生成的文件将放入此目录中
-p <pkg> 指定目标程序包
-httpproxy <proxy> 设置 HTTP/HTTPS 代理。格式为 [user[:password]@]proxyHost:proxyPort
-httpproxyfile <f> 作用与 -httpproxy 类似, 但在文件中采用参数来保护口令
-classpath <arg> 指定查找用户类文件的位置
-catalog <file> 指定用于解析外部实体引用的目录文件支持 TR9401, XCatalog 和 OASIS XML 目录格式。
-readOnly 生成的文件将处于只读模式
-npa 禁止生成程序包级别注释 (**/package-info.java)
-no-header 禁止生成带有时间戳的文件头
-target (2.0|2.1) 行为与 XJC 2.0 或 2.1 类似, 用于生成不使用任何 2.2 功能的代码。
-encoding <encoding> 为生成的源文件指定字符编码
-enableIntrospection 用于正确生成布尔型 getter/setter 以启用 Bean 自测 apis
-contentForWildcard 为具有多个 xs:any 派生元素的类型生成内容属性
-xmlschema 采用 W3C XML 模式处理输入 (默认值)
-relaxng 采用 RELAX NG 处理输入 (实验性的, 不支持)
-relaxng-compact 采用 RELAX NG 简洁语法处理输入 (实验性的, 不支持)
-dtd 采用 XML DTD 处理输入 (实验性的, 不支持)
-wsdl 采用 WSDL 处理输入并编译其中的模式 (实验性的, 不支持)
-verbose 特别详细
-quiet 隐藏编译器输出
-help 显示此帮助消息
-version 显示版本信息
-fullversion 显示完整的版本信息
(1)查看 xjc 命令的帮助信息,如下:
D:\share_dir\ShareDoc> xjc -help 用法: xjc [-options ...] <schema file/URL/dir/jar> ... [-b <bindinfo>] ... 如果指定 dir, 将编译该目录中的所有模式文件。 如果指定 jar, 将编译 /META-INF/sun-jaxb.episode 绑定文件。 选项: ...
(2)将指定的 schema1.xsd XML 模式文件生成相应的 Java 类,如下:
D:\share_dir\ShareDoc> xjc schema1.xsd 正在解析模式... 正在编译模式... generated\ObjectFactory.java generated\Person.java generated\SchemagenDemo.java
命令执行成功后,将在当前目录创建一个 generated 目录,存放生产的 Java 文件,它们的内容如下:
generated\ObjectFactory.java:
//
// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.8-b130911.1802 生成的
// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// 在重新编译源模式时, 对此文件的所有修改都将丢失。
// 生成时间: 2024.07.07 时间 11:11:04 AM CST
//
package generated;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the generated package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
private final static QName _Person_QNAME = new QName("", "person");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: generated
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link Person }
*
*/
public Person createPerson() {
return new Person();
}
/**
* Create an instance of {@link SchemagenDemo }
*
*/
public SchemagenDemo createSchemagenDemo() {
return new SchemagenDemo();
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link Person }{@code >}}
*
*/
@XmlElementDecl(namespace = "", name = "person")
public JAXBElement<Person> createPerson(Person value) {
return new JAXBElement<Person>(_Person_QNAME, Person.class, null, value);
}
}generated\Person.java:
//
// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.8-b130911.1802 生成的
// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// 在重新编译源模式时, 对此文件的所有修改都将丢失。
// 生成时间: 2024.07.07 时间 11:11:04 AM CST
//
package generated;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>person complex type的 Java 类。
*
* <p>以下模式片段指定包含在此类中的预期内容。
*
* <pre>
* <complexType name="person">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="age" type="{http://www.w3.org/2001/XMLSchema}int"/>
* <element name="name" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "person", propOrder = {
"age",
"name"
})
public class Person {
protected int age;
protected String name;
/**
* 获取age属性的值。
*
*/
public int getAge() {
return age;
}
/**
* 设置age属性的值。
*
*/
public void setAge(int value) {
this.age = value;
}
/**
* 获取name属性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* 设置name属性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
}generated\SchemagenDemo.java:
//
// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.8-b130911.1802 生成的
// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// 在重新编译源模式时, 对此文件的所有修改都将丢失。
// 生成时间: 2024.07.07 时间 11:11:04 AM CST
//
package generated;
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.XmlElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>schemagenDemo complex type的 Java 类。
*
* <p>以下模式片段指定包含在此类中的预期内容。
*
* <pre>
* <complexType name="schemagenDemo">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="peoples" type="{}person" maxOccurs="unbounded" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "schemagenDemo", propOrder = {
"peoples"
})
public class SchemagenDemo {
@XmlElement(nillable = true)
protected List<Person> peoples;
/**
* Gets the value of the peoples property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the peoples property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getPeoples().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link Person }
*
*
*/
public List<Person> getPeoples() {
if (peoples == null) {
peoples = new ArrayList<Person>();
}
return this.peoples;
}
}(2)指定生成的 Java 类所在的包名为 com.hxstrive.xjc,如下:
D:\share_dir\ShareDoc> xjc -p com.hxstrive.xjc schema1.xsd 正在解析模式... 正在编译模式... com\hxstrive\xjc\ObjectFactory.java com\hxstrive\xjc\Person.java com\hxstrive\xjc\SchemagenDemo.java
(3)将生成的 Java 类输出到指定的目录 output,如下:
D:\share_dir\ShareDoc> xjc -d output schema1.xsd 正在解析模式... 正在编译模式... generated\ObjectFactory.java generated\Person.java generated\SchemagenDemo.java
执行成功,输出如下图:

注意:如果指定的输出目录不存在,则会出现“拒绝写入不存在的目录 "output"”错误。
(4)同时指定包名和输出目录,如下:
D:\share_dir\ShareDoc> xjc -p com.hxstrive.xjc -d output schema1.xsd 正在解析模式... 正在编译模式... com\hxstrive\xjc\ObjectFactory.java com\hxstrive\xjc\Person.java com\hxstrive\xjc\SchemagenDemo.java
注意:如果指定的输出目录不存在,则会出现“拒绝写入不存在的目录 "output"”错误。