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.
Difference between revisions of "EclipseLink/DesignDocs/293925/MOXyExtensions/XMLDirectMapping"
< EclipseLink | DesignDocs | 293925 | MOXyExtensions
(→Deployment XML) |
|||
Line 77: | Line 77: | ||
<attribute-name>firstName</attribute-name> | <attribute-name>firstName</attribute-name> | ||
<field name="name/first-name/text()" xsi:type="node"/> | <field name="name/first-name/text()" xsi:type="node"/> | ||
+ | </attribute-mapping> | ||
+ | <attribute-mapping xsi:type="xml-direct-mapping"> | ||
+ | <attribute-name>lasstName</attribute-name> | ||
+ | <field name="name/last-name/text()" xsi:type="node"/> | ||
</attribute-mapping> | </attribute-mapping> | ||
<attribute-mapping xsi:type="xml-direct-mapping"> | <attribute-mapping xsi:type="xml-direct-mapping"> |
Revision as of 17:01, 2 February 2010
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);
Deployment XML
<class-mapping-descriptor xsi:type="xml-class-mapping-descriptor"> <class>org.example.Employee</class> <alias>Employee</alias> <attribute-mappings> <attribute-mapping xsi:type="xml-direct-mapping"> <attribute-name>empId</attribute-name> <field name="@id" xsi:type="node"/> </attribute-mapping> <attribute-mapping xsi:type="xml-direct-mapping"> <attribute-name>firstName</attribute-name> <field name="name/first-name/text()" xsi:type="node"/> </attribute-mapping> <attribute-mapping xsi:type="xml-direct-mapping"> <attribute-name>lasstName</attribute-name> <field name="name/last-name/text()" xsi:type="node"/> </attribute-mapping> <attribute-mapping xsi:type="xml-direct-mapping"> <attribute-name>projectName</attribute-name> <field name="prj:project/text()" xsi:type="node"/> </attribute-mapping> <attribute-mapping xsi:type="xml-direct-mapping"> <attribute-name>mgrId</attribute-name> <field name="prj:project/@managerId" xsi:type="node"/> </attribute-mapping> </attribute-mappings> <descriptor-type>aggregate</descriptor-type> <default-root-element>employee</default-root-element> <default-root-element-field name="employee" xsi:type="node"/> <namespace-resolver> <namespaces> <namespace> <prefix>prj</prefix> <namespace-uri>http://www.example.com/projects</namespace-uri> </namespace> </namespaces> <default-namespace-uri>http://www.example.com/employees</default-namespace-uri> </namespace-resolver> </class-mapping-descriptor>
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>
XML Instance Document
<?xml version="1.0" encoding="UTF-8"?> <employee id="66" xmlns="http://www.example.com/employees" xmlns:prj="http://www.example.com/projects"> <prj:project managerId="99">XML External Metadata Support</prj:project> <name> <first-name>Joe</first-name> <last-name>Black</last-name> </name> </employee>