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
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.
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.
If you're using Eclipse, you can also try out JAXB Eclipse Plug-In
You can find more information about XJC Binding Compiler that comes with the jdk installation over here: xjc: Java™ Architecture for XML Binding -Binding Compiler
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.
Source: Stackoverflow.com