使用jdk的xjc命令由schema文件生成相应的实体类
1、使用方法
xjc fileName.xsd -d 生成java实体类的目录 -p 生成的包名
2. eg: xjc catalog.xsd -d -p com.xjc.bean
3、其他的参数
-httpproxy <proxy> : set HTTP/HTTPS proxy. Format is [user[:password]@]proxyH
ost:proxyPort
-httpproxyfile <f> : Works like -httpproxy but takes the argument in a file t
o protect password
-classpath <arg> : specify where to find user class files
-catalog <file> : specify catalog files to resolve external entity referen
ces
support TR9401, XCatalog, and OASIS XML Catalog format.
-readOnly : generated files will be in read-only mode
-npa : suppress generation of package level annotations (**/pac
kage-info.java)
-no-header : suppress generation of a file header with timestamp
-target (2.0|2.1) : behave like XJC 2.0 or 2.1 and generate code that doesnt
use any 2.2 features.
-encoding <encoding> : specify character encoding for generated source files
-enableIntrospection : enable correct generation of Boolean getters/setters t
o enable Bean Introspection apis
-contentForWildcard : generates content property for types with multiple xs:
any derived elements
-xmlschema : treat input as W3C XML Schema (default)
-relaxng : treat input as RELAX NG (experimental,unsupported)
-relaxng-compact : treat input as RELAX NG compact syntax (experimental,uns
upported)
-dtd : treat input as XML DTD (experimental,unsupported)
-wsdl : treat input as WSDL and compile schemas inside it (exper
imental,unsupported)
-verbose : be extra verbose
-quiet : suppress compiler output
-help : display this help message
-version : display version information
-fullversion : display full version information
Extensions:
-Xinject-code : inject specified Java code fragments into the generated
code
-Xlocator : enable source location support for generated code
-Xsync-methods : generate accessor methods with the 'synchronized' keywor
d
-mark-generated : mark the generated code as @javax.annotation.Generated
-episode <FILE> : generate the episode file for separate compilation
4. 例如
xjc catalog.xsd -d . -p cn.heima.catalog.schema.entity
catalog.xsd文件生成java实体类
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.itcast.org/catalog" xmlns:tns="http://www.itcast.org/catalog" elementFormDefault="qualified"> <element name="catalog"> <complexType> <sequence> <element name="infoNode" type="tns:infoNode" minOccurs="0" maxOccurs="unbounded"></element> <element ref="tns:catalog" minOccurs="0" maxOccurs="unbounded"></element> </sequence> <attribute name="id" use="required" type="string"></attribute> <attribute name="title" use="required" type="string"></attribute> </complexType> </element> <complexType name="infoNode"> <attribute name="id" use="required" type="string"></attribute> <attribute name="infonodeid" use="required" type="string"></attribute> </complexType> </schema>5、生成的4个类
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2013.06.09 at 09:28:16 PM CST // package cn.heima.catalog.schema.entity; 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.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; /** * <p>Java class for anonymous complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="infoNode" type="{http://www.itcast.org/catalog}infoNode" maxOccurs="unbounded" minOccurs="0"/> * <element ref="{http://www.itcast.org/catalog}catalog" maxOccurs="unbounded" minOccurs="0"/> * </sequence> * <attribute name="id" use="required" type="{http://www.w3.org/2001/XMLSchema}string" /> * <attribute name="title" use="required" type="{http://www.w3.org/2001/XMLSchema}string" /> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "infoNode", "catalog" }) @XmlRootElement(name = "catalog") public class Catalog { protected List<InfoNode> infoNode; protected List<Catalog> catalog; @XmlAttribute(name = "id", required = true) protected String id; @XmlAttribute(name = "title", required = true) protected String title; /** * Gets the value of the infoNode 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 infoNode property. * * <p> * For example, to add a new item, do as follows: * <pre> * getInfoNode().add(newItem); * </pre> * * * <p> * Objects of the following type(s) are allowed in the list * {@link InfoNode } * * */ public List<InfoNode> getInfoNode() { if (infoNode == null) { infoNode = new ArrayList<InfoNode>(); } return this.infoNode; } /** * Gets the value of the catalog 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 catalog property. * * <p> * For example, to add a new item, do as follows: * <pre> * getCatalog().add(newItem); * </pre> * * * <p> * Objects of the following type(s) are allowed in the list * {@link Catalog } * * */ public List<Catalog> getCatalog() { if (catalog == null) { catalog = new ArrayList<Catalog>(); } return this.catalog; } /** * Gets the value of the id property. * * @return * possible object is * {@link String } * */ public String getId() { return id; } /** * Sets the value of the id property. * * @param value * allowed object is * {@link String } * */ public void setId(String value) { this.id = value; } /** * Gets the value of the title property. * * @return * possible object is * {@link String } * */ public String getTitle() { return title; } /** * Sets the value of the title property. * * @param value * allowed object is * {@link String } * */ public void setTitle(String value) { this.title = value; } }
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2013.06.09 at 09:28:16 PM CST // package cn.heima.catalog.schema.entity; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlType; /** * <p>Java class for infoNode complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType name="infoNode"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <attribute name="id" use="required" type="{http://www.w3.org/2001/XMLSchema}string" /> * <attribute name="infonodeid" use="required" type="{http://www.w3.org/2001/XMLSchema}string" /> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "infoNode") public class InfoNode { @XmlAttribute(name = "id", required = true) protected String id; @XmlAttribute(name = "infonodeid", required = true) protected String infonodeid; /** * Gets the value of the id property. * * @return * possible object is * {@link String } * */ public String getId() { return id; } /** * Sets the value of the id property. * * @param value * allowed object is * {@link String } * */ public void setId(String value) { this.id = value; } /** * Gets the value of the infonodeid property. * * @return * possible object is * {@link String } * */ public String getInfonodeid() { return infonodeid; } /** * Sets the value of the infonodeid property. * * @param value * allowed object is * {@link String } * */ public void setInfonodeid(String value) { this.infonodeid = value; } }
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2013.06.09 at 09:28:16 PM CST // package cn.heima.catalog.schema.entity; import javax.xml.bind.annotation.XmlRegistry; /** * This object contains factory methods for each * Java content interface and Java element interface * generated in the cn.heima.catalog.schema.entity 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 { /** * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: cn.heima.catalog.schema.entity * */ public ObjectFactory() { } /** * Create an instance of {@link Catalog } * */ public Catalog createCatalog() { return new Catalog(); } /** * Create an instance of {@link InfoNode } * */ public InfoNode createInfoNode() { return new InfoNode(); } }
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2013.06.09 at 09:28:16 PM CST // @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.itcast.org/catalog", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package cn.heima.catalog.schema.entity;