How to create pojo classes from XSD?

。_饼干妹妹 提交于 2019-12-03 13:24:54

问题


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.

What is a correct way to generate classes from xsd in java?

below is 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>

回答1:


My recommendation is to go with 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.

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 video tutorial
  • Youtube tutorial using maven

I hope it helps!

Feel free to comment if you encounter any issue.




回答2:


jaxb2-maven-plugin

Using jaxb2-maven-plugin is the easiest way. Define the plugins as below :

<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




回答3:


One simple way to convert .xsd files to Java file is xjc tool. Just execute the following command in the same working directory:

xjc test.xsd


来源:https://stackoverflow.com/questions/33731896/how-to-create-pojo-classes-from-xsd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!