Java 如何从 XSD 创建 pojo 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33731896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create pojo classes from XSD?
提问by Chaitanya Ghumare
I am using Spring maven plugin, I want to create POJO classes from specified xml schema in particular folder. I tried with xjc
command through java code, but its not generating that classes. secondly, I tried with jaxb
, but its dealing with xml
file not a xsd
schema while marshell/unmarshelling. I think this not a way to create POJO from xsd
.
我正在使用 Spring maven 插件,我想从特定文件夹中的指定 xml 模式创建 POJO 类。我尝试xjc
通过 java 代码使用命令,但它没有生成该类。其次,我尝试使用jaxb
,但它在编组/解组时处理xml
文件而不是xsd
模式。我认为这不是从xsd
.
What is a correct way to generate classes from xsd in java?
在java中从xsd生成类的正确方法是什么?
below is 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>
采纳答案by SyntaX
My recommendation is to go with
JAXB
.
我的建议是去
JAXB
。
I have tested it in eclipse
, works well for me. My suggestion is try generating the POJO from command line
or with the help of eclipse
. Once successful configure it with maven
to generate the POJO build time
.
我已经在 中进行了测试eclipse
,对我来说效果很好。我的建议是尝试从生成POJOcommand line
或与帮助eclipse
。一旦成功配置它maven
以生成 POJO build time
。
There are several tutorials to learn this, please follow the below link(s) based upon your preference:
有几个教程可以学习这一点,请根据您的喜好点击以下链接:
- Generate POJO Class from XSD in Eclipse
- Generate POJO class from XSD Schema command line
- Generate POJO Classes from XSD using
XJC
Maven Plugin
Also the youtube links:
还有youtube链接:
I hope it helps!
我希望它有帮助!
Feel free to comment if you encounter any issue.
如果您遇到任何问题,请随时发表评论。
回答by Arpit Aggarwal
回答by AzizSM
jaxb2-maven-plugin
jaxb2-maven-插件
Using jaxb2-maven-plugin is the easiest way. Define the plugins as below :
使用 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>
and execute :
并执行:
mvn jaxb2:xjc
the generated files will be located in target\generated-sources\jaxb
生成的文件将位于 target\generated-sources\jaxb