將Schema檔案轉換為Java檔案

林竟發表於2013-11-30

可通過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>

 

相關文章