Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/DesignDocs/293925/MOXyExtensions/XMLDirectMapping

XMLDirectMapping

Provide support for XML direct mappings via xml-element and xml-attribute.

The following table shows ...

Example: Configure an XMLDirectMapping

The following example will demonstrate how to configure an XMLDirectMapping via XML Metadata:

Java Code

XMLDirectMapping idMapping = new XMLDirectMapping();
idMapping.setAttributeName("empId");
idMapping.setXPath("@id");
 
XMLDirectMapping firstNameMapping = new XMLDirectMapping();
firstNameMapping.setAttributeName("firstName");
firstNameMapping.setXPath("name/first-name/text()");
 
XMLDirectMapping lastNameMapping = new XMLDirectMapping();
lastNameMapping.setAttributeName("lastName");
lastNameMapping.setXPath("name/last-name/text()");
 
XMLDirectMapping mgrIdMapping = new XMLDirectMapping();
mgrIdMapping.setAttributeName("mgrId");
mgrIdMapping.setXPath("prj:project/@managerId");
 
XMLDirectMapping projectNameMapping = new XMLDirectMapping();
lastNameMapping.setAttributeName("projectName");
lastNameMapping.setXPath("prj:project/text()");
 
NamespaceResolver namespaceResolver = new NamespaceResolver();
namespaceResolver.setDefaultNamespaceURI("http://www.example.com/employees")
namespaceResolver.put("prj", "http://www.example.com/projects");
 
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(Employee.class);
descriptor.setDefaultRootElement("employee");
descriptor.setNamespaceResolver(namespaceResolver);
descriptor.addMapping(idMapping);
descriptor.addMapping(firstNameMapping);
descriptor.addMapping(lastNameMapping);
descriptor.addMapping(mgrIdMapping);
descriptor.addMapping(projectNameMapping);

XML Metadata

<?xml version="1.0" encoding="US-ASCII"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/
    <xml-schema namespace="http://www.example.com/employees">
        <xml-ns namespace-uri="http://www.example.com/projects" prefix="prj"/>
    </xml-schema>
    <java-types>
        <java-type name="org.eclipse.persistence.testing.jaxb.externalizedmetadata.xmlpath.Employee">
            <xml-root-element name="employee" />
            <java-attributes>
                <xml-attribute java-attribute="empId" name="id" />
                <xml-element java-attribute="firstName" xml-path="name/first-name/text()"/>
                <xml-element java-attribute="lastName" xml-path="name/last-name/text()"/>
                <xml-attribute java-attribute="mgrId" xml-path="prj:project/@managerId"/>
                <xml-element java-attribute="projectName" xml-path="prj:project/text()"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

Back to the top