将Schema文件转换为Java文件

来源:互联网 发布:ipad上传淘宝宝贝软件 编辑:程序博客网 时间:2024/06/01 08:53

可通过xjc命令完成将schema文件转换为java文件。

打开命令控制台,切换至项目中xsd文件所在目录,如E:\Eclipse\webservice\03_schema\src\schema

输入命令:xjc -d <导出的java文件存放目录> -verbose <需要转换的xsd文件>

如:xjc -d E:\Eclipse\webserviceimport\02 -verbose classroom.xsd

生成的Java文件:

生成的ClassroomType.java

//// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4 // 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.11.30 at 11:10:46 PM CST //package org.example.classroom;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>Java class for classroomType complex type. *  * <p>The following schema fragment specifies the expected content contained within this class. *  * <pre> * <complexType name="classroomType"> *   <complexContent> *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> *       <sequence> *         <element name="grade" type="{http://www.example.org/classroom}gradeType"/> *         <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/> *         <sequence maxOccurs="unbounded"> *           <element name="student" type="{http://www.example.org/classroom}studentType"/> *         </sequence> *       </sequence> *     </restriction> *   </complexContent> * </complexType> * </pre> *  *  */@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "classroomType", propOrder = {    "grade",    "name",    "student"})public class ClassroomType {    protected int grade;    @XmlElement(required = true)    protected String name;    @XmlElement(required = true)    protected List<StudentType> student;    /**     * Gets the value of the grade property.     *      */    public int getGrade() {        return grade;    }    /**     * Sets the value of the grade property.     *      */    public void setGrade(int value) {        this.grade = value;    }    /**     * Gets the value of the name property.     *      * @return     *     possible object is     *     {@link String }     *          */    public String getName() {        return name;    }    /**     * Sets the value of the name property.     *      * @param value     *     allowed object is     *     {@link String }     *          */    public void setName(String value) {        this.name = value;    }    /**     * Gets the value of the student 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 student property.     *      * <p>     * For example, to add a new item, do as follows:     * <pre>     *    getStudent().add(newItem);     * </pre>     *      *      * <p>     * Objects of the following type(s) are allowed in the list     * {@link StudentType }     *      *      */    public List<StudentType> getStudent() {        if (student == null) {            student = new ArrayList<StudentType>();        }        return this.student;    }}

生成的java文件还是很完备的。

---

classroom.xsd

<?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/classroom"xmlns:tns="http://www.example.org/classroom" elementFormDefault="qualified"><xsd:include schemaLocation="student.xsd" /><xsd:element name="classroom" type="tns:classroomType" /><xsd:complexType name="classroomType"><xsd:sequence><xsd:element name="grade" type="tns:gradeType" /><xsd:element name="name" type="xsd:string" /><!--如果需要生成java文件,最好不要多重嵌套,此处改用sequence <xsd:element name="students"><xsd:complexType><xsd:sequence minOccurs="1" maxOccurs="unbounded"><xsd:element name="student" type="tns:studentType" /></xsd:sequence></xsd:complexType></xsd:element> --><xsd:sequence minOccurs="1" maxOccurs="unbounded"><xsd:element name="student" type="tns:studentType" /></xsd:sequence></xsd:sequence></xsd:complexType><xsd:simpleType name="gradeType"><xsd:restriction base="xsd:int"><xsd:minInclusive value="2008" /><xsd:maxInclusive value="3000" /></xsd:restriction></xsd:simpleType></xsd:schema>

 

原创粉丝点击