JAX-SB is a Java framework for binding to XML Schemas. The JAX-SB framework provides a complete solution for cohesive integration of Java applications to XML Documents via XML Schemas. The framework provides a JAX-SB Java Source Code Generator and a JAX-SB Runtime to parse and marshal XML Documents to and from Java objects. The JAX-SB framework is a complete implementation and covers the entirety of the XSD 1.1 Specification.
JAX-SB supports all directives of the XSD Specification, and generates bindings with highest degree of cohesion possible with the Java language. JAX-SB supports the following:
JAX-SB provides complete binding to the namespaces and types defined in XML Schemas (import
and include
), and preserves prefix definitions.
JAX-SB provides binding to simpleType
, complexType
, element
, group
, attribute
, attributeGroup
, notation
, any
and anyAttribute
structural types.
JAX-SB provides binding to annotation
, documentation
, key
, keyref
and unique
.
JAX-SB provides binding to xs:type
, xs:ref
, all
, sequence
, choice
,
JAX-SB provides binding to the complexContent
, restriction
, extension
, list
and union
facets.
JAX-SB provides binding to the enumeration
, pattern
and [min|max][Inclusive|Exclusive]
.
JAX-SB properly binds to elements that use the xsi:type
and redefine
directives, which is represented by Java's class inheritance model.
-
Create a
example.xsd
XML Schema and put it insrc/main/resources/
.<xs:schema elementFormDefault="qualified" targetNamespace="http://mycompany.com/app/example.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="example"> <xs:complexType> <xs:sequence> <xs:element name="hello"> <xs:complexType> <xs:attribute name="world" type="xs:boolean" use="required"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
-
Add the
org.jaxsb:jaxsb-maven-plugin
to the POM.<plugin> <groupId>org.jaxsb</groupId> <artifactId>jaxsb-maven-plugin</artifactId> <version>2.2.0</version> <executions> <execution> <goals> <goal>generate</goal> </goals> <configuration> <destDir>${project.build.directory}/generated-sources/jaxsb</destDir> <schemas> <schema>src/main/resources/example.xsd</schema> </schemas> </configuration> </execution> </executions> </plugin>
-
Add the
org.jaxsb:jaxsb-runtime
dependency to the POM.<dependency> <groupId>org.jaxsb</groupId> <artifactId>runtime</artifactId> <version>2.2.0</version> </dependency>
-
Run
mvn install
. Upon successful execution of thejaxsb-maven-plugin
, a new path will be generated intarget/generated-sources/jaxsb
. Add this path to your Build Paths in your IDE to integrate into your project. A class by the name ofcom.mycompany.app.example.xe
contains the bindings toexample.xsd
. -
Create a XML Document in
src/test/resources
.<example xmlns="http://mycompany.com/app/example.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://mycompany.com/app/example.xsd ../../main/resources/example.xsd"> <hello world="true"/> </example>
-
To parse the XML file into Java objects. In your
App.java
:ex_example example = (ex_example)Bindings.parse(new InputSource(Resources.getResourceOrFile("example.xml").getURL().openStream()));
-
To marshal JAX-SB Java objects to a XML String:
System.out.println(DOMs.domToString(Bindings.marshal(example), DOMStyle.INDENT));
Samples are provided in the JAX-SB source code:
git clone git@github.com:jaxsb/jaxsb.git
cd jaxsb/sample
mvn install
Tutorials are provided in the JAX-SB source code:
git clone git@github.com:jaxsb/jaxsb.git
cd jaxsb/tutorial
mvn install
The JAX-SB framework is not suitable for processing of large XML Documents (in the 100s of MBs). Built on top of Java's org.w3c.dom
implementation of the Document Object Model (DOM) Level 3 Core Specification, XML Documents parsed and marshalled by JAX-SB consume a significant amount of memory. Work is underway for JAX-SB v3 to use Java's SAX Parser, which will allow the framework to process XML Documents of unbounded size.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
This project is licensed under the MIT License - see the LICENSE.txt file for details.