[java] How to generate JAXB classes from XSD?

I'm a total newbie with XML. I'm doing a Java EE project REST implementation and we return a lot of XML. With this we decided to use JAXB. So far, we manually coded the Models for the XML.

But there are already these complex structures we don't know how to code. We've read about generating classes from XSD. We do have an XSD.

My questions:

1.) I've read about XJC, where can I find it?

2.) Do we have to install the whole JAXB? (so what we used so far? isn't this JAXB?)

This question is related to java jakarta-ee jaxb

The answer is


cxf does great support for this kind of stuff e.g

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-xjc-plugin</artifactId>
    <version>2.3.0</version>
    <configuration>
      <extensions>
        <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:2.3.0</extension>
      </extensions>
    </configuration>
    <executions>
      <execution>
        <id>generate-sources-trans</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>xsdtojava</goal>
        </goals>
        <configuration>
          <sourceRoot>${basedir}/src/main/java</sourceRoot>
          <xsdOptions>
            <xsdOption>
              <xsd>src/main/resources/xxx.xsd</xsd>
            </xsdOption>
          </xsdOptions>
        </configuration>
      </execution>
    </executions>
  </plugin>

You can also generate source code from schema using jaxb2-maven-plugin plugin:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <sources>
                    <source>src/main/resources/your_schema.xsd</source>
                </sources>
                <xjbSources>
                    <xjbSource>src/main/resources/bindings.xjb</xjbSource>
                </xjbSources>
                <packageName>some_package</packageName>
                <outputDirectory>src/main/java</outputDirectory>
                <clearOutputDir>false</clearOutputDir>
                <generateEpisode>false</generateEpisode>
                <noGeneratedHeaderComments>true</noGeneratedHeaderComments>
            </configuration>
        </plugin>

You can download the JAXB jar files from http://jaxb.java.net/2.2.5/ You don't need to install anything, just invoke the xjc command and with classpath argument pointing to the downloaded JAXB jar files.


  1. Download http://java.net/downloads/jaxb-workshop/IDE%20plugins/org.jvnet.jaxbw.zip
  2. Extract the zip file .
  3. Place the org.jvnet.jaxbw.eclipse_1.0.0 folder into .eclipse\plugins folder
  4. Restart the eclipse.
  5. Right click on XSD file and you can find contect menu. JAXB 2.0 -> Run XJC .

In Eclipse, right click on the xsd file you want to get --> Generate --> Java... --> Generator: "Schema to JAXB Java Classes".

I just faced the same problem, I had a bunch of xsd files, only one of them being the XML Root Element and it worked well what I explained above in Eclipse


In intellij click .xsd file -> WebServices ->Generate Java code from Xml Schema JAXB then give package path and package name ->ok


1) You can use standard java utility xjc - ([your java home dir]\bin\xjc.exe). But you need to create .bat (or .sh) script for using it.

e.g. generate.bat:

[your java home dir]\bin\xjc.exe %1 %2 %3

e.g. test-scheme.xsd:

<?xml version="1.0"?>
<xs:schema version="1.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified" 
           targetNamespace="http://myprojects.net/xsd/TestScheme"
           xmlns="http://myprojects.net/xsd/TestScheme">
    <xs:element name="employee" type="PersonInfoType"/>

    <xs:complexType name="PersonInfoType">
        <xs:sequence>
            <xs:element name="firstname" type="xs:string"/>
            <xs:element name="lastname" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

Run .bat file with parameters: generate.bat test-scheme.xsd -d [your src dir]

For more info use this documentation - http://docs.oracle.com/javaee/5/tutorial/doc/bnazg.html

and this - http://docs.oracle.com/javase/6/docs/technotes/tools/share/xjc.html

2) JAXB (xjc utility) is installed together with JDK6 by default.


I hope this helps!


For Eclipse STS (3.5 at least) you don't need to install anything. Right click on schema.xsd -> Generate -> JAXB Classes. You'll have to specify the package & location in the next step and that's all, your classes should be generated. I guess all the above mentioned solutions work, but this seems by far the easiest (for STS users).

[UPDATE] Eclipse STS version 3.6 (based on Kepler) comes with the same functionality.

blah


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to jakarta-ee

Java 11 package javax.xml.bind does not exist javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure The type java.io.ObjectInputStream cannot be resolved. It is indirectly referenced from required .class files Deploying Maven project throws java.util.zip.ZipException: invalid LOC header (bad signature) web.xml is missing and <failOnMissingWebXml> is set to true WELD-001408: Unsatisfied dependencies for type Customer with qualifiers @Default Name [jdbc/mydb] is not bound in this Context An internal error occurred during: "Updating Maven Project". java.lang.NullPointerException How to consume a SOAP web service in Java java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

Examples related to jaxb

Java 11 package javax.xml.bind does not exist Java: How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException Convert Java object to XML string no suitable HttpMessageConverter found for response type javax.xml.bind.UnmarshalException: unexpected element. Expected elements are (none) java.lang.VerifyError: Expecting a stackmap frame at branch target JDK 1.7 javax.xml.bind.JAXBException: Class *** nor any of its super class is known to this context How to generate JAXB classes from XSD? convert xml to java object using jaxb (unmarshal) I can't understand why this JAXB IllegalAnnotationException is thrown