Skip to main content

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.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Runtime/Dynamic/Customizing"

m (Replacing page with 'See http://www.eclipse.org/eclipselink/documentation/2.4/moxy/advanced_concepts009.htm')
 
Line 1: Line 1:
{{EclipseLink_UserGuide
+
See http://www.eclipse.org/eclipselink/documentation/2.4/moxy/advanced_concepts009.htm
|info=y
+
|eclipselink=y
+
|eclipselinktype=MOXy
+
|api=y
+
|apis= * [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/jaxb/dynamic/DynamicJAXBContextFactory.html DynamicJAXBContextFactory]
+
}}
+
 
+
= Customizing Generated Mappings with EclipseLink Metadata =
+
 
+
When bootstrapping from an XML Schema (or an EclipseLink project from sessions.xml), you can customize the mappings that EclipseLink generates by using your own EclipseLink OXM Bindings file. This file contains your additional mappings and allows you to combine OXM with XSD bootstrapping. This means that you can use EclipseLink mappings to customize an existing XML schema.
+
 
+
== Example ==
+
 
+
This example shows how to override mappings defined in the schema. Although the schema defines addresses in Canadian format (with '''province''' and '''postal code'''), you can use XML that contains the address is USA format (with '''state''' and '''zip code''').
+
 
+
First, you must create an '''eclipselink-oxm.xml''' file that contains the mapping overrides. In this example, we modify the XPaths for '''province''' and '''postalCode''':
+
 
+
<source lang="xml">
+
<?xml version="1.0" encoding="US-ASCII"?>
+
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="example">
+
    <java-types>
+
        <java-type name="Address">
+
            <java-attributes>
+
                <xml-element java-attribute="province" xml-path="state/text()"/>
+
                <xml-element java-attribute="postalCode" xml-path="zip-code/text()"/>
+
            </java-attributes>
+
        </java-type>
+
    </java-types>
+
</xml-bindings>
+
</source>
+
 
+
When you create a '''DynamicJAXBContext''', use the '''properties''' argument to pass this binding file to the '''DynamicJAXBContextFactory''' (in addition to the Schema):
+
 
+
<source lang="java">
+
// Load Schema
+
InputStream xsdStream = myClassLoader.getSystemResourceAsStream("example/resources/xsd/customer.xsd");
+
+
// Load OXM with customizations, put into Properties
+
InputStream oxmStream = myClassLoader.getSystemResourceAsStream("example/resources/eclipselink/eclipselink-oxm.xml");
+
Map<String, Object> props = new HashMap<String, Object>();
+
props.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, oxmStream);
+
+
// Create Context
+
DynamicJAXBContext dContext = DynamicJAXBContextFactory.createContextFromXSD(inputStream, null, myClassLoader, props);
+
</source>
+

Latest revision as of 10:34, 8 November 2012

See http://www.eclipse.org/eclipselink/documentation/2.4/moxy/advanced_concepts009.htm

Back to the top