20

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>
Chaitanya Ghumare
  • 351
  • 2
  • 6
  • 18
  • 1
    We use a tool called XMLBeans in our production code. It is deprecated, but has worked well for us. – Tim Biegeleisen Nov 16 '15 at 09:21
  • can you please explain more. how to use it? – Chaitanya Ghumare Nov 16 '15 at 09:30
  • 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](https://docs.oracle.com/javase/8/docs/technotes/tools/windows/xjc.html) command yourself? – Andreas Nov 16 '15 at 09:31
  • As @Andreas mentioned, you can't create classes at runtime, but you can create classes during the build. See [here](https://xmlbeans.apache.org/) 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. – Tim Biegeleisen Nov 16 '15 at 09:33
  • @Andreas 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. – Chaitanya Ghumare Nov 16 '15 at 09:48
  • 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(); – Chaitanya Ghumare Nov 16 '15 at 09:49
  • 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. – Andreas Nov 16 '15 at 09:51
  • 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 XSD – Chaitanya Ghumare Nov 16 '15 at 09:55
  • classes from XSD can (and MUST) be autogenerated by your IDE using JAXB plugin.... http://www.javaworld.com/article/2071784/enterprise-java/java-xml-mapping-made-easy-with-jaxb-2-0.html – Jordi Castilla Nov 16 '15 at 10:06
  • `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. – SyntaX Nov 16 '15 at 10:22
  • 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 XSD – Chaitanya Ghumare Nov 16 '15 at 10:29
  • Possible duplicate of http://stackoverflow.com/questions/11463231/how-to-generate-jaxb-classes-from-xsd – Hulk Nov 16 '15 at 10:35

4 Answers4

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.

Community
  • 1
  • 1
SyntaX
  • 2,090
  • 17
  • 30
  • 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 ? – Chaitanya Ghumare Nov 17 '15 at 06:06
  • 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! – SyntaX Nov 17 '15 at 06:17
  • 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? – Chaitanya Ghumare Nov 17 '15 at 06:28
  • Please let me know if it is not helping you! – SyntaX Nov 17 '15 at 06:30
  • 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 working – Chaitanya Ghumare Nov 17 '15 at 06:47
  • 1
    Thank you for the third link :) exactly what I needed – Rishi Nov 04 '19 at 08:37
  • looks like the page refer by 3rd link is move [https://www.digitalocean.com/community/tutorials/jaxb2-maven-plugin-xjc-example-generate-java-classes-xsd](https://www.digitalocean.com/community/tutorials/jaxb2-maven-plugin-xjc-example-generate-java-classes-xsd) – Minnow Nov 04 '22 at 06:29
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

AzizSM
  • 6,199
  • 4
  • 42
  • 53
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
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
  • Here is an example of xjc https://thoughts-on-java.org/generate-your-jaxb-classes-in-second/ – DOKKA Mar 09 '19 at 06:56
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

JustinKSU
  • 4,875
  • 2
  • 29
  • 51