How to create pojo classes from XSD?
Asked Answered
P

4

22

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>
Parthia answered 16/11, 2015 at 9:19 Comment(12)
We use a tool called XMLBeans in our production code. It is deprecated, but has worked well for us.Monostome
can you please explain more. how to use it?Parthia
You can't "create classes at run time". You can however create classes at build time (or manually before that) from an XML Schema (XSD) file, using XJC. I don't know how you do that using maven, though, but have you tried running the xjc command yourself?Admass
As @Admass mentioned, you can't create classes at runtime, but you can create classes during the build. See here for more information. Keep in mind that XMLBeans was retired as of about one year ago. So you might want to use a more modern framework.Monostome
@Admass yes I will change my question it should generate at build time. and I tried with xjc its working when I am manually firing that command on cmd but through java code its not generating that classes.Parthia
String[] createPojo= new String [] { "CMD.EXE", "/C", "cd \"C:\\training_1\\mongo-starter\" && dir","xjc -d target -p generated-sources Xmlschema.xsd" }; Process runtimeProcess = Runtime.getRuntime().exec(createPojo); int processComplete = runtimeProcess.waitFor();Parthia
What do you mean "through java code"? Why are you running Java code at build time? The build should be done with Ant, Maven, Gradle, or other build tool, and the build produces Java class files to be executed at run time.Admass
through java code only I want to generated classes from the given XSD. XSD will changes so I want to configure something which through I can genrated that pojo class according to XSDParthia
classes from XSD can (and MUST) be autogenerated by your IDE using JAXB plugin.... javaworld.com/article/2071784/enterprise-java/…Facelift
JAXB has to work whichever may be the way you trying, you seem to be doing something wrong. Why don't you share the xsd file, we can try to generate POJO from it and get back.Thema
yes @Bikram Kundu Actually I am not getting how to use jaxb through java code for pojo creation I dont want to create it manually by creating jaxb project then using that generate pojo functions of IDE.I am adding XSDParthia
Possible duplicate of #11463731Caseate
T
31

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:

Also the youtube links:

I hope it helps!

Feel free to comment if you encounter any issue.

Thema answered 16/11, 2015 at 12:9 Comment(7)
yes I gone thrugh this all. I dont want to create those classes manually or by the cmd it should be generate from java code I am using Spring .Actually if our XSD file is same then it works fine but in my case XSD file will change time to time user will give us a xsd file and from that I want to create classes so I dont want to generate it manually.is there any onather way? can we run that cmd command through Spring ?Parthia
I have mentioned about how to achieve it in build time using maven, this should help you. Check the third link! I don't think you can achieve it run time, at least I am not aware of anything like that!Thema
yes In third link we have to configure pakage path and file then it will generate those classes at build time. but is ther any onather way to configure without using plugins?Parthia
Please let me know if it is not helping you!Thema
Its Helping me but my requirement is little bit different instead of configuring in plugin I want to take that XSD file from user . so I tried to run that XJC command through java code but that is not workingParthia
Thank you for the third link :) exactly what I neededFilch
looks like the page refer by 3rd link is move https://www.digitalocean.com/community/tutorials/jaxb2-maven-plugin-xjc-example-generate-java-classes-xsdDeepset
A
8

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

Abranchiate answered 15/7, 2019 at 3:36 Comment(1)
Awesomely simple. Thanks mateSiusan
T
6

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
Tiv answered 18/2, 2019 at 9:43 Comment(1)
Here is an example of xjc thoughts-on-java.org/generate-your-jaxb-classes-in-secondBillowy
S
2

jaxb2-maven-plugin version 2 changes how the configure.

The following will run xjc on everything in src/main/resource and put it 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>

Check out the implicit behavior in https://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.5.0/example_xjc_basic.html

Stockwell answered 8/2, 2021 at 22:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.