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.
EclipseLink/DesignDocs/293925/MOXyExtensions/XMLDirectMapping
< EclipseLink | DesignDocs | 293925 | MOXyExtensions
XMLDirectMapping
Provide support for XML direct mappings via xml-element and xml-attribute.
Example: Configure an XMLDirectMapping
The following example will demonstrate how to configure an XMLDirectMapping via XML Metadata:
org.example.Employee.java
package org.example; public class Employee { public int empId; public int mgrId; public String firstName; public String lastName; public String projectName; }
org.example.EmployeeProject.java (project class snippet)
// setup direct mappings 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()"); // setup namespace resolver NamespaceResolver namespaceResolver = new NamespaceResolver(); namespaceResolver.setDefaultNamespaceURI("http://www.example.com/employees") namespaceResolver.put("prj", "http://www.example.com/projects"); // setup descriptor 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/oxm"> <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.example.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>