【问题标题】:How to create pojo classes from XSD?如何从 XSD 创建 pojo 类?
【发布时间】:2015-11-16 09:19:25
【问题描述】:

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

在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>

【问题讨论】:

  • 我们在生产代码中使用了一个名为 XMLBeans 的工具。它已被弃用,但对我们来说效果很好。
  • 你能解释一下吗?怎么用?
  • 您不能“在运行时创建类”。但是,您可以使用 XJC 在构建时(或在此之前手动)从 XML 模式 (XSD) 文件创建类。不过,我不知道您是如何使用 maven 做到这一点的,但是您是否尝试过自己运行 xjc 命令?
  • 正如@Andreas 提到的,您不能在运行时创建类,但可以在构建期间创建类。有关更多信息,请参阅here。请记住,大约一年前,XMLBeans 已停用。因此,您可能希望使用更现代的框架。
  • @Andreas 是的,我将更改它应该在构建时生成的问题。当我在 cmd 上手动触发该命令时,我尝试使用 xjc 它的工作,但通过 java 代码它不会生成该类。

标签: java spring maven xsd pojo


【解决方案1】:

我的建议是使用JAXB

我已经在eclipse 中测试过,对我来说效果很好。我的建议是尝试从command line 或在eclipse 的帮助下生成 POJO。成功后使用maven 配置它以生成POJO build time

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

还有 youtube 链接:

希望对你有帮助!

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

【讨论】:

  • 是的,我经历了这一切。我不想手动或通过 cmd 创建这些类,它应该从我使用 Spring 的 java 代码生成。实际上,如果我们的 XSD 文件相同,那么它可以正常工作,但在我的情况下,XSD 文件会不时更改用户会给我们一个xsd文件,我想从中创建类,所以我不想手动生成它。还有其他方法吗?我们可以通过 Spring 运行那个 cmd 命令吗?
  • 我已经提到了如何在构建时使用 maven 实现它,这应该对你有所帮助。检查第三个链接!我不认为你可以实现它的运行时间,至少我不知道这样的事情!
  • yes 在第三个链接中,我们必须配置包路径和文件,然后它将在构建时生成这些类。但是有没有其他不使用插件的配置方式?
  • 如果对您没有帮助,请告诉我!
  • 感谢第三个链接 :) 正是我所需要的
【解决方案2】:

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

xjc test.xsd

【讨论】:

【解决方案3】:

jaxb2-maven-plugin

使用 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>

并执行:

mvn jaxb2:xjc

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

【讨论】:

  • 非常简单。谢谢老哥
【解决方案4】:

jaxb2-maven-plugin 第 2 版更改了配置方式。

以下将对src/main/resource中的所有内容运行xjc并将其放入com.yourcompany.xsd

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>2.5.0</version>
    <executions>
        <execution>
            <goals>
                <goal>xjc</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <sources>
            <source>src/main/resources</source>
        </sources>
        <packageName>com.yourcompany.xsd</packageName>
    </configuration>
</plugin>

查看https://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.5.0/example_xjc_basic.html 中的隐式行为

【讨论】:

    猜你喜欢
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    相关资源
    最近更新 更多