From Eclipsepedia

Jump to: navigation, search


In this example you will learn how to bootstrap a dynamic JAXBContext from an XML Schema.

Bootstrap the JAXBContext

The DynamicJAXBContextFactory is used to create a dyanic JAXBContext. There are several parameters to the create method, but in this example we will focus on the InputStream representing the XML schema.

import javax.xml.bind.JAXBContext;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
public class Demo {
    public static void main(String[] args) throws Exception {
        FileInputStream xsdInputStream = new FileInputStream("src/example/dynamic/customer.xsd");
        JAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);

XML Schema

The following XML schema represents the metadata for this JAXBContext.

<xsd:schema xmlns:xsd="" xmlns:tns="urn:example" targetNamespace="urn:example">
   <xsd:complexType name="address">
         <xsd:element name="city" type="xsd:string" minOccurs="0"/>
         <xsd:element name="street" type="xsd:string" minOccurs="0"/>
   <xsd:complexType name="customer">
         <xsd:element name="name" type="xsd:string" minOccurs="0"/>
         <xsd:element name="address" type="tns:address" minOccurs="0"/>
   <xsd:element name="customer" type="tns:customer"/>