Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/MOXy/Dynamic/JAXBContextFromXMLSchema"

< EclipseLink‎ | Examples‎ | MOXy‎ | Dynamic
(Bootstrap the JAXBContext)
(XML Schema)
Line 28: Line 28:
 
<xsd:schema  
 
<xsd:schema  
 
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
 
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
   xmlns="www.example.org/customer"  
+
   xmlns="www.example.org"  
   targetNamespace="www.example.org/customer"
+
   targetNamespace="www.example.org"
 
   elementFormDefault="qualified">
 
   elementFormDefault="qualified">
 
+
 
   <xsd:complexType name="address">
 
   <xsd:complexType name="address">
 
       <xsd:sequence>
 
       <xsd:sequence>
 +
        <xsd:element name="street" type="xsd:string" minOccurs="0"/>
 
         <xsd:element name="city" type="xsd:string" minOccurs="0"/>
 
         <xsd:element name="city" type="xsd:string" minOccurs="0"/>
        <xsd:element name="street" type="xsd:string" minOccurs="0"/>
 
      </xsd:sequence>
 
  </xsd:complexType>
 
 
  <xsd:complexType name="customer">
 
      <xsd:sequence>
 
        <xsd:element name="name" type="xsd:string" minOccurs="0"/>
 
        <xsd:element name="address" type="address" minOccurs="0"/>
 
 
       </xsd:sequence>
 
       </xsd:sequence>
 
   </xsd:complexType>
 
   </xsd:complexType>
 
+
   <xsd:element name="customer" type="customer"/>
+
   <xsd:element name="customer">
 
+
      <xsd:complexType>
 +
        <xsd:sequence>
 +
            <xsd:element name="name" type="xsd:string" minOccurs="0"/>
 +
            <xsd:element name="address" type="address" minOccurs="0"/>
 +
        </xsd:sequence>
 +
      </xsd:complexType>
 +
  </xsd:element>
 +
 
</xsd:schema></source>
 
</xsd:schema></source>

Revision as of 10:57, 21 June 2010

Overview

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 java.io.FileInputStream;
 
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
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");
        DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);
    }
 
}

XML Schema

The following XML schema represents the metadata for this JAXBContext.

<xsd:schema 
   xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
   xmlns="www.example.org" 
   targetNamespace="www.example.org"
   elementFormDefault="qualified">
 
   <xsd:complexType name="address">
      <xsd:sequence>
         <xsd:element name="street" type="xsd:string" minOccurs="0"/>
         <xsd:element name="city" type="xsd:string" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
 
   <xsd:element name="customer">
      <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="name" type="xsd:string" minOccurs="0"/>
            <xsd:element name="address" type="address" minOccurs="0"/>
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>
 
</xsd:schema>