Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/From Schema"
m (→Bootstrapping from XML Schema (XSD)) |
m (→Bootstrapping from XML Schema (XSD)) |
||
Line 64: | Line 64: | ||
public static DynamicJAXBContext createContextFromXSD(javax.xml.transform.Source schemaSource, EntityResolver resolver, | public static DynamicJAXBContext createContextFromXSD(javax.xml.transform.Source schemaSource, EntityResolver resolver, | ||
ClassLoader classLoader, Map<String, ?> properties) throws JAXBException | ClassLoader classLoader, Map<String, ?> properties) throws JAXBException | ||
+ | |||
+ | </source> | ||
+ | |||
+ | |||
+ | {{tip||The <tt>classLoader</tt> parameter is your application's current class loader, and will be used to first lookup classes to see if they exist before new <tt>DynamicTypes</tt> are generated. The user may pass in null for this parameter, and <tt>Thread.currentThread().getContextClassLoader()</tt> will be used instead.}} | ||
+ | |||
+ | |||
+ | {{tip||The <tt>properties</tt> parameter can be used to pass additional properties to <tt>DynamicJAXBContextFactory</tt>. For more information on these properties see [[#Advanced Topics|Advanced Topics]].}} | ||
+ | |||
+ | |||
+ | === Example === | ||
+ | |||
+ | This example shows how to create and marshall a new object using Dynamic MOXy. | ||
+ | |||
+ | |||
+ | Sample XML Schema: | ||
+ | <source lang="xml"> | ||
+ | |||
+ | <?xml version="1.0" encoding="UTF-8"?> | ||
+ | <xs:schema targetNamespace="mynamespace" xmlns:myns="mynamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" | ||
+ | attributeFormDefault="qualified" elementFormDefault="qualified"> | ||
+ | |||
+ | <xs:element name="customer" type="myns:customer"/> | ||
+ | |||
+ | <xs:complexType name="customer"> | ||
+ | <xs:sequence> | ||
+ | <xs:element name="first-name" type="xs:string"/> | ||
+ | <xs:element name="last-name" type="xs:string"/> | ||
+ | <xs:element name="address" type="myns:address"/> | ||
+ | </xs:sequence> | ||
+ | </xs:complexType> | ||
+ | |||
+ | <xs:complexType name="address"> | ||
+ | <xs:sequence> | ||
+ | <xs:element name="street" type="xs:string"/> | ||
+ | <xs:element name="city" type="xs:string"/> | ||
+ | <xs:element name="province" type="xs:string"/> | ||
+ | <xs:element name="postal-code" type="xs:string"/> | ||
+ | </xs:sequence> | ||
+ | </xs:complexType> | ||
+ | |||
+ | </xs:schema> | ||
+ | |||
+ | </source> | ||
+ | |||
+ | The following code: | ||
+ | * Passes the XML Schema to <tt>DynamicJAXBContextFactory</tt> to create a <tt>DynamicJAXBContext</tt> | ||
+ | * Creates new <tt>DynamicEntities</tt> and setting their properties | ||
+ | * Creates a <tt>JAXBMarshaller</tt> and marshalling the Java objects to XML | ||
+ | |||
+ | <source lang="java"> | ||
+ | |||
+ | InputStream inputStream = myClassLoader.getSystemResourceAsStream("mynamespace/resources/xsd/customer.xsd"); | ||
+ | DynamicJAXBContext dContext = DynamicJAXBContextFactory.createContextFromXSD(inputStream, null, myClassLoader, null); | ||
+ | |||
+ | DynamicEntity newCustomer = dContext.newDynamicEntity("mynamespace.Customer"); | ||
+ | newCustomer.set("firstName", "George"); | ||
+ | newCustomer.set("lastName", "Jones"); | ||
+ | |||
+ | DynamicEntity newAddress = dContext.newDynamicEntity("mynamespace.Address"); | ||
+ | newAddress.set("street", "227 Main St."); | ||
+ | newAddress.set("city", "Toronto"); | ||
+ | newAddress.set("province", "Ontario"); | ||
+ | newAddress.set("postalCode", "M5V1E6"); | ||
+ | |||
+ | newCustomer.set("address", newAddress); | ||
+ | |||
+ | dContext.createMarshaller().marshal(newCustomer, System.out); | ||
</source> | </source> |
Revision as of 09:33, 3 January 2011
EclipseLink MOXy
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Key API
Contents
Specifying Bootstrapping from an XML Schema
As with conventional JAXB, the first step is to create a JAXBContext. This is achieved by use of the DynamicJAXBContextFactory class. A DynamicJAXBContext cannot be instantiated directly; it must be created through the factory API.
A DynamicJAXBContext can be created from:
- an XML Schema file (XSD)
- the EclipseLink OXM metadata file
- an EclipseLink Project specified in the EclipseLink sessions.xml file
Bootstrapping from XML Schema (XSD)
With EclipseLink MOXy, you can provide an existing XML schema from which to create a DynamicJAXBContext. EclipseLink will parse the schema and generate DynamicTypes for each complex type.
EclipseLink MOXy uses Sun's XJC (XML-to-Java Compiler) APIs to parse the schema into an in-memory representation and generate DynamicTypes and Mappings.
You can pass the XML Schema to DynamicJAXBContextFactory by using:
- InputStream
- Node
- Source
The following example demonstrates these methods:
/** * Create a DynamicJAXBContext, using XML Schema as the metadata source. * * @param schemaStream * java.io.InputStream from which to read the XML Schema. * @param resolver * An org.xml.sax.EntityResolver, used to resolve schema imports. Can be null. * @param classLoader * The application's current class loader, which will be used to first lookup * classes to see if they exist before new DynamicTypes are generated. Can be * null, in which case Thread.currentThread().getContextClassLoader() will be used. * @param properties * Map of properties to use when creating a new DynamicJAXBContext. Can be null. * * @return * A new instance of DynamicJAXBContext. * * @throws JAXBException * if an error was encountered while creating the DynamicJAXBContext. */ public static DynamicJAXBContext createContextFromXSD(java.io.InputStream schemaStream, EntityResolver resolver, ClassLoader classLoader, Map<String, ?> properties) throws JAXBException public static DynamicJAXBContext createContextFromXSD(org.w3c.dom.Node schemaDOM, EntityResolver resolver, ClassLoader classLoader, Map<String, ?> properties) throws JAXBException public static DynamicJAXBContext createContextFromXSD(javax.xml.transform.Source schemaSource, EntityResolver resolver, ClassLoader classLoader, Map<String, ?> properties) throws JAXBException
Example
This example shows how to create and marshall a new object using Dynamic MOXy.
Sample XML Schema:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema targetNamespace="mynamespace" xmlns:myns="mynamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified"> <xs:element name="customer" type="myns:customer"/> <xs:complexType name="customer"> <xs:sequence> <xs:element name="first-name" type="xs:string"/> <xs:element name="last-name" type="xs:string"/> <xs:element name="address" type="myns:address"/> </xs:sequence> </xs:complexType> <xs:complexType name="address"> <xs:sequence> <xs:element name="street" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="province" type="xs:string"/> <xs:element name="postal-code" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema>
The following code:
- Passes the XML Schema to DynamicJAXBContextFactory to create a DynamicJAXBContext
- Creates new DynamicEntities and setting their properties
- Creates a JAXBMarshaller and marshalling the Java objects to XML
InputStream inputStream = myClassLoader.getSystemResourceAsStream("mynamespace/resources/xsd/customer.xsd"); DynamicJAXBContext dContext = DynamicJAXBContextFactory.createContextFromXSD(inputStream, null, myClassLoader, null); DynamicEntity newCustomer = dContext.newDynamicEntity("mynamespace.Customer"); newCustomer.set("firstName", "George"); newCustomer.set("lastName", "Jones"); DynamicEntity newAddress = dContext.newDynamicEntity("mynamespace.Address"); newAddress.set("street", "227 Main St."); newAddress.set("city", "Toronto"); newAddress.set("province", "Ontario"); newAddress.set("postalCode", "M5V1E6"); newCustomer.set("address", newAddress); dContext.createMarshaller().marshal(newCustomer, System.out);
Importing other Schemas / EntityResolvers
Customizing Generated Mappings with EclipseLink Metadata