<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="address">
<xs:sequence>
<xs:element name="street" type="xs:string" minOccurs="0"/>
<xs:element name="zip" type="xs:string" minOccurs="0"/>
<xs:element name="city" type="xs:string" minOccurs="0"/>
<xs:element name="country" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
D:\guosen_workspace\jaxb-test\src\SchemaToJava.java
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.sun.tools.xjc.XJCFacade;
/**
*
*/
/**
* @author jack
*
*/
public class SchemaToJava {
/**
* @param args
* @throws Throwable
*/
public static void main(String[] args) throws Throwable {
String projectPath = new File("").getAbsolutePath();
//xsd File
String xsdFilePath = projectPath +"\\src\\testSchema.xsd";
//生成的Java文件路径
String destDir = projectPath+"\\src";
//JAXB描述文件
String jaxbFile =
projectPath+"\\src\\jaxb_desc.xml";
List<String> argsList = new ArrayList<String>();
//一次性可以添加多个XSD File
argsList.add(xsdFilePath);
//添加生成的Java文件路径
argsList.add("-d");
argsList.add(destDir);
//添加JAXB描述文件
argsList.add("-b");
argsList.add(jaxbFile);
//生成代码
String[] argsq = new String[argsList.size()];
XJCFacade.main(argsList.toArray(argsq));
}
}
D:\guosen_workspace\jaxb-test\src\jaxb_desc.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<jaxb:bindings schemaLocation="testSchema.xsd"
node="/xs:schema">
<jaxb:schemaBindings
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<jaxb:package name="com.jackguo.xsdgenerated.test" />
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
D:\guosen_workspace\jaxb-test\src\com\jackguo\xsdgenerated\test\Address.java
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-257
// 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: 2012.03.17 at 01:17:51 下午 CST
//
package com.jackguo.xsdgenerated.test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for address complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="address">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="street" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="zip" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="city" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="country" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "address", propOrder = {
"street",
"zip",
"city",
"country"
})
public class Address {
protected String street;
protected String zip;
protected String city;
protected String country;
/**
* Gets the value of the street property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getStreet() {
return street;
}
/**
* Sets the value of the street property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setStreet(String value) {
this.street = value;
}
/**
* Gets the value of the zip property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getZip() {
return zip;
}
/**
* Sets the value of the zip property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setZip(String value) {
this.zip = value;
}
/**
* Gets the value of the city property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getCity() {
return city;
}
/**
* Sets the value of the city property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setCity(String value) {
this.city = value;
}
/**
* Gets the value of the country property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getCountry() {
return country;
}
/**
* Sets the value of the country property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setCountry(String value) {
this.country = value;
}
}
D:\guosen_workspace\jaxb-test\src\com\jackguo\xsdgenerated\test\ObjectFactory.java
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-257
// 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: 2012.03.17 at 01:17:51 下午 CST
//
package com.jackguo.xsdgenerated.test;
import javax.xml.bind.annotation.XmlRegistry;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the com.jackguo.xsdgenerated.test 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: com.jackguo.xsdgenerated.test
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link Address }
*
*/
public Address createAddress() {
return new Address();
}
}