A short introduction and references about the “Java Architecture for XML Binding” (JAXB).
JAXB is a Java standard and a tool that permit to generate, from an XML schema, a set of Java classes. Those classes represent the XML types defined in the schema. These classes can then be used to easily convert data from and to XML documents conforming to the schema.
(JAXB also permits to go the other way around: generate, from Java classes, an XML schema, but this document does not focus on this aspect.)
We can now use the generated classes to easily write XML instances conforming to the schema, a process known as marshalling.
Exercice: marshal a simple XML document using the generated classes. See this example code.
You can also use the jaxb context object to go the other way around: create an unmarshaller, and obtain instances of the Java classes from an XML instance.
You can customize the class generation with an XML file with extension xjb.
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:jaxbnp="http://jaxb2-commons.dev.java.net/namespace-prefix" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd http://jaxb2-commons.dev.java.net/namespace-prefix https://raw.githubusercontent.com/javaee/jaxb2-commons/master/namespace-prefix/src/main/resources/prefix-namespace-schema.xsd" version="2.0" schemaLocation="example-shema.xsd"> <jaxb:schemaBindings> <jaxb:package name="my.packg.name" /> </jaxb:schemaBindings> </jaxb:bindings>
Exercice: use xjc again, this time specifying that the customization file (containing the above content) must be used. You need to specify as schemaLocation the example schema that you apply xjc to.
Exercice: change your code for generation of an XML instance (what do you need to change)? Test that it still works.
Maven can generate the classes for you from the schema. See for example this sample POM (only the build element is involved in the classes generation). When configured in this manner, Maven will call xjc for you as part of the build process.
Exercice: integrate automatic generation to your own project.
To generate nice prefixes for better readability of the generated XML instances, add this to your customization.
<jaxb:bindings> <jaxbnp:prefix name="myprefix" /> </jaxb:bindings>
This is what the dependency to org.jvnet.jaxb2_commons:jaxb2-namespace-prefix is for in the sample POM above. (If you do not use the “nice prefix” functionality, you can remove this dependency.)
JAXB specification 2.3 defines http://java.sun.com/xml/ns/jaxb as the namespace defining the schema for binding declarations (Section 7.1); and gives URL http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd for the online version of the JAXB Binding Schema (appendix C).
-
Full working examples: simplest, with customization (and biggest schema), with more complex schema.
-
See Oracle Tutorial, Oracle Docs, API.
-
In Eclipse, you should probably disable the validator (see bug).