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

From Eclipsepedia

< EclipseLink‎ | Examples‎ | MOXy‎ | Dynamic
Jump to: navigation, search
(New page: <source lang="java"> package example.dynamic; import java.io.FileInputStream; import javax.xml.bind.JAXBContext; import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory; pu...)
 
Line 1: Line 1:
<source lang="java">
+
==Overview==
package example.dynamic;
+
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.
 +
 +
<source lang="java">
 
import java.io.FileInputStream;
 
import java.io.FileInputStream;
 
import javax.xml.bind.JAXBContext;
 
import javax.xml.bind.JAXBContext;
Line 13: Line 17:
 
     }
 
     }
 
}
 
}
 +
</source>
 +
 +
==XML Schema==
 +
 +
The following XML schema represents the metadata for this JAXBContext.
 +
 +
<source lang="xml">
 +
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="urn:example" targetNamespace="urn:example">
 +
  <xsd:complexType name="address">
 +
      <xsd:sequence>
 +
        <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="tns:address" minOccurs="0"/>
 +
      </xsd:sequence>
 +
  </xsd:complexType>
 +
  <xsd:element name="customer" type="tns:customer"/>
 +
</xsd:schema>
 
</source>
 
</source>

Revision as of 16:49, 17 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 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="http://www.w3.org/2001/XMLSchema" xmlns:tns="urn:example" targetNamespace="urn:example">
   <xsd:complexType name="address">
      <xsd:sequence>
         <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="tns:address" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="customer" type="tns:customer"/>
</xsd:schema>