如何从XSD创建pojo类?

Cha*_*are 10 java spring xsd pojo maven

我正在使用Spring maven插件,我想从特定文件夹中的指定xml架构创建POJO类.我试过xjc通过java代码的命令,但它没有生成那些类.其次,我试过jaxb,但它处理xml文件而不是xsdmarshell/unmarshelling模式.我认为这不是一种创建POJO的方法xsd.

在java中从xsd生成类的正确方法是什么?

下面是XSD

   <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="Employee">
   <xs:complexType>
   <xs:sequence>
    <xs:element name="empId" type="xs:long"/>
    <xs:element name="lastName" type="xs:string"/>
    <xs:element name="title" type="xs:string"/>
    <xs:element name="salary" type="xs:integer"/>
    <xs:element name="address">
    <xs:complexType>
       <xs:sequence>
         <xs:element name="city" type="xs:string"/>
         <xs:element name="street" type="xs:string"/>
         <xs:element name="zipcode" type="xs:integer"/>
         <xs:element name="privatePhoneNo">
           <xs:complexType>
             <xs:sequence>
                 <xs:element name="privateMobile" type="xs:string"/>
                 <xs:element name="privateLandline" type="xs:string"/>
             </xs:sequence>
           </xs:complexType>
         </xs:element>
        </xs:sequence>
     </xs:complexType>
    </xs:element>
 </xs:sequence>
 </xs:complexType>
 </xs:element>
 </xs:schema>
Run Code Online (Sandbox Code Playgroud)

Syn*_*taX 18

我的建议是去JAXB.

我测试过它eclipse,对我来说效果很好.我的建议是尝试从command line或通过帮助生成POJO eclipse.成功配置后maven,生成POJO build time.

有几个教程可以学习,请根据您的偏好点击以下链接:

也是youtube链接:

我希望它有所帮助!

如果您遇到任何问题,请随时发表评论.


Azi*_*zSM 8

jaxb2-maven-插件

使用 jaxb2-maven-plugin 是最简单的方法。定义插件如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaDirectory>${project.basedir}/src/main/xsd/</schemaDirectory>
                <schemaFiles>MARC21slim.xsd</schemaFiles>
            </configuration>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

并执行:

mvn jaxb2:xjc
Run Code Online (Sandbox Code Playgroud)

生成的文件将位于 target\generated-sources\jaxb


Arp*_*wal 5

.xsd文件转换为 Java 文件的一种简单方法是xjc工具。只需在同一工作目录中执行以下命令:

xjc test.xsd
Run Code Online (Sandbox Code Playgroud)