= 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 ==
* http://docs.oracle.com/javaee/5/tutorial/doc/bnazq.html
|| '''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 ==
{{{#!highlight sh
cd /tmp
mkdir -p xjctest/xsd
cd xjctest
}}}
nano simple.xjb
{{{#!highlight xml
}}}
'''nano xsd/test.xsd'''
{{{#!highlight xml
}}}
Run command
{{{#!highlight sh
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 ==
{{{#!highlight sh
* mkdir -p /tmp/xjctest/src/main/resources
}}}
=== pom.xml ===
{{{#!highlight xml
4.0.0
org.allowed.bitarus
xjctest
0.1.0
org.jvnet.jaxb2.maven2
maven-jaxb2-plugin
0.11.0
generate
src/main/resources
*.xsd
src/main/resources
*.xjb
}}}
* 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/
== Generate classes with jaxb in Java 17 ==
Will generate files to src/main/java with package test.org.types.Client from the xsd file.
* test/org/types/Client.java
* test/org/types/ObjectFactory.java
* org/w3/_2001/xmlschema/Adapter1.java
{{{#!highlight sh
cd ~
wget https://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-ri/4.0.2/jaxb-ri-4.0.2.zip
yum install unzip
unzip jaxb-ri-4.0.2.zip
jaxb-ri/bin/xjc.sh
mkdir ~/xjctest
cd xjctest
mkdir -p src/main/resources
mkdir -p src/main/java
~/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 ===
{{{#!highlight xml
4.0.0
org.allowed.bitarus
xjctest
0.1.0
jakarta.xml.bind
jakarta.xml.bind-api
4.0.0
javax.xml.bind
jaxb-api
2.3.1
}}}
=== src/main/resources/simple.xjb ===
{{{#!highlight xml
}}}
=== src/main/resources/test.xsd ===
{{{#!highlight xml
}}}