jaxb
http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.5/tutorial/doc/JAXBUsing.html
- With JAXB you can:
- Generate JAXB Java classes from an XML schema
- Use schema-derived JAXB classes to unmarshal and marshal XML content in a Java application
- Create a Java content tree from scratch using schema-derived JAXB classes
- Validate XML content during unmarshalling and at runtime
- Customize JAXB schema-to-Java bindings
XSD data types
XSD type |
Java type |
xsd:string |
java.lang.String |
xsd:integer |
java.math.BigInteger |
xsd:int |
int |
xsd.long |
long |
xsd:short |
short |
xsd:decimal |
java.math.BigDecimal |
xsd:float |
float |
xsd:double |
double |
xsd:boolean |
boolean |
xsd:byte |
byte |
xsd:QName |
javax.xml.namespace.QName |
xsd:dateTime |
javax.xml.datatype.XMLGregorianCalendar |
xsd:base64Binary |
byte[] |
xsd:hexBinary |
byte[] |
xsd:unsignedInt |
long |
xsd:unsignedShort |
int |
xsd:unsignedByte |
short |
xsd:time |
javax.xml.datatype.XMLGregorianCalendar |
xsd:date |
javax.xml.datatype.XMLGregorianCalendar |
xsd:g |
javax.xml.datatype.XMLGregorianCalendar |
xsd:anySimpleType |
java.lang.Object |
xsd:anySimpleType |
java.lang.String |
xsd:duration |
javax.xml.datatype.Duration |
xsd:NOTATION |
javax.xml.namespace.QName |
xjc command generation example
- cd /tmp
- mkdir -p xjctest/xsd
- cd xjctest
nano simple.xjb
1 <jaxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
2 xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
3 xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
4 jaxb:extensionBindingPrefixes="xjc"
5 jaxb:version="2.0">
6 <jaxb:globalBindings generateValueClass="true">
7 <xjc:simple />
8 <xjc:serializable uid="12343"/> <!-- marks generated class as Serializable-->
9 <jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
10 parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
11 printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
12 </jaxb:globalBindings>
13 </jaxb:bindings>
nano xsd/test.xsd
1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2 <xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
3 <xs:element name="test" type="Test"/>
4 <xs:complexType name="Test">
5 <xs:sequence>
6 <xs:element name="name" type="xs:string" minOccurs="0"/>
7 <xs:element name="birthDate" type="xs:dateTime" minOccurs="0"/>
8 <xs:element name="dummyInt" type="xs:int" minOccurs="0"/>
9 </xs:sequence>
10 </xs:complexType>
11 </xs:schema>
Run command
- xjc xsd/test.xsd -b simple.xjb -extension -npa
parsing a schema... compiling a schema... generated/ObjectFactory.java generated/Test.java org/w3/_2001/xmlschema/Adapter1.java
To generate a proper Date it might be necessary to use an adapter for xjb.
Generate Java classes with Maven, xjc and XSD
- mkdir -p /tmp/xjctest/src/main/resources
nano pom.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5 http://maven.apache.org/xsd/maven-4.0.0.xsd">
6 <modelVersion>4.0.0</modelVersion>
7 <groupId>org.allowed.bitarus</groupId>
8 <artifactId>xjctest</artifactId>
9 <version>0.1.0</version>
10 <build>
11 <plugins>
12 <plugin>
13 <groupId>org.jvnet.jaxb2.maven2</groupId>
14 <artifactId>maven-jaxb2-plugin</artifactId>
15 <version>0.11.0</version>
16 <executions>
17 <execution>
18 <goals>
19 <goal>generate</goal>
20 </goals>
21 </execution>
22 </executions>
23 <configuration>
24 <schemaDirectory>src/main/resources</schemaDirectory>
25 <schemaIncludes>
26 <include>*.xsd</include>
27 </schemaIncludes>
28 <bindingDirectory>src/main/resources</bindingDirectory>
29 <bindingIncludes>
30 <include>*.xjb</include>
31 </bindingIncludes>
32 </configuration>
33 </plugin>
34 </plugins>
35 </build>
36 </project>
- Copy test.xsd and and simple.xjb to /tmp/xjctest/src/main/resources
- run mvn clean install
- The source code is created in /tmp/xjctest/target/generated-sources/xjc/