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.
EclipseLink/UserGuide/MOXy/Runtime/Dynamic/Customizing
EclipseLink MOXy
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Key API
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.
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:
<?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>
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);