ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 使用 Eclipse 从 JAXB Java 类生成 XSD > 原文: [https://howtodoinjava.com/jaxb/java-class-to-xsd-eclipse/](https://howtodoinjava.com/jaxb/java-class-to-xsd-eclipse/) 学习**使用 Eclipse IDE 从带有 JAXB 注解的 Java 类**创建 XML 模式文档(xsd)。 ## 1)将 JAXB 注解添加到 Java 类 第一步是向 Java 类添加`@XmlRootElement`,`@XmlAccessorType`和`@XmlElement`等注解。 ```java import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "employee") @XmlAccessorType(XmlAccessType.PROPERTY) public class Employee implements Serializable { private static final long serialVersionUID = 1L; private Integer id; private String firstName; private String lastName; private Department department; public Employee() { super(); } //Setters and Getters } ``` ```java import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "department") @XmlAccessorType(XmlAccessType.PROPERTY) public class Department implements Serializable { private static final long serialVersionUID = 1L; Integer id; String name; public Department() { super(); } //Setters and Getters } ``` ## 2)从 JAXB 类生成 XSD #### 2.1)导航到 Eclipse 选项 ```java File -> New -> JAXB -> Schema from JAXB Classes ``` ![Schema from JAXB Classes Option](https://img.kancloud.cn/8f/36/8f36a5bb4a6083438e873da4ee97b22b_633x394.png) JAXB 类选项的模式 ## 2.2)选择生成的模式文件的位置 ![Locaion of Generated Schema File](https://img.kancloud.cn/9b/e8/9be8b2457707034df18e76c32e8e19d9_562x455.png) 生成的模式文件的位置 #### 2.3)选择 JAXB 类 ![Choose JAXB Classes](https://img.kancloud.cn/0f/d1/0fd1178efca43a450f024889b65a5cb1_629x447.png) 选择 JAXB 类 #### 2.4)生成 xsd 文件 ```java <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="department" type="department"/> <xs:element name="employee" type="employee"/> <xs:complexType name="employee"> <xs:sequence> <xs:element ref="department" minOccurs="0"/> <xs:element name="firstName" type="xs:string" minOccurs="0"/> <xs:element name="id" type="xs:int" minOccurs="0"/> <xs:element name="lastName" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> <xs:complexType name="department"> <xs:sequence> <xs:element name="id" type="xs:int" minOccurs="0"/> <xs:element name="name" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:schema> ``` XSd 文件已生成,现在您可以将其用于各种应用用例。 学习愉快!