jaxb

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

   1 cd /tmp
   2 mkdir -p xjctest/xsd
   3 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

   1 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

   1  * mkdir -p /tmp/xjctest/src/main/resources

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>

Generate classes with jaxb in Java 17

Will generate files to src/main/java with package test.org.types.Client from the xsd file.

   1 cd ~
   2 wget https://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-ri/4.0.2/jaxb-ri-4.0.2.zip
   3 yum install unzip 
   4 unzip jaxb-ri-4.0.2.zip
   5 jaxb-ri/bin/xjc.sh
   6 mkdir ~/xjctest
   7 cd xjctest
   8 mkdir -p src/main/resources
   9 mkdir -p src/main/java
  10 ~/jaxb-ri/bin/xjc.sh src/main/resources/test.xsd -b src/main/resources/simple.xjb -d src/main/java -p org.example -extension -npa

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   <dependencies>
  11     <dependency>
  12       <groupId>jakarta.xml.bind</groupId>
  13       <artifactId>jakarta.xml.bind-api</artifactId>
  14       <version>4.0.0</version>
  15     </dependency>
  16     <dependency>
  17       <groupId>javax.xml.bind</groupId>
  18       <artifactId>jaxb-api</artifactId>
  19       <version>2.3.1</version>
  20     </dependency>
  21   </dependencies>
  22 </project>

src/main/resources/simple.xjb

   1 <jaxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
   2            xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
   3            xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
   4            jaxb:extensionBindingPrefixes="xjc"
   5            jaxb:version="3.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>

src/main/resources/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" targetNamespace="http://org.test/types" >
   3   <xs:complexType name="Client">
   4     <xs:sequence>
   5       <xs:element name="name" type="xs:string" minOccurs="0"/>
   6       <xs:element name="creationDate" type="xs:dateTime" minOccurs="0"/>
   7       <xs:element name="id" type="xs:int" minOccurs="0"/>
   8     </xs:sequence>
   9   </xs:complexType>
  10 </xs:schema>

Java/jaxb (last edited 2023-05-29 08:53:16 by 127)