Skip to main content
Jump to: navigation, search


EclipseLink MOXy

Mailing ListForumsIRCmattermost
OpenHelp WantedBug Day
Browse Source

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 'and' XSD bootstrapping. This means that you can use EclipseLink mappings to customize an existing XML schema.


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:

<?xml version="1.0" encoding="US-ASCII"?>
<xml-bindings xmlns="" package-name="example">
        <java-type name="Address">
                <xml-element java-attribute="province" xml-path="state/text()"/>
                <xml-element java-attribute="postalCode" xml-path="zip-code/text()"/>

When you create a DynamicJAXBContext, use the properties argument to pass this binding file to the DynamicJAXBContextFactory (in addition to the Schema):

// 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);

Back to the top