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 1: | Line 1: | ||
<div style="border: 1px solid rgb(0, 0, 0); margin: 5px; padding: 5px; float: right;">__TOC__</div> | <div style="border: 1px solid rgb(0, 0, 0); margin: 5px; padding: 5px; float: right;">__TOC__</div> | ||
− | = XMLDirectMapping = | + | = XMLDirectMapping = |
− | Provide support for XML direct mappings via xml-element and xml-attribute. | + | Provide support for XML direct mappings via xml-element and xml-attribute. |
− | == | + | == Open Issues == |
− | + | * XPath - should we overload the 'name' attribute on xml-attribute and xml-element to set the XPath, or create a new attribute 'xml-path'? | |
− | === org.example.Employee.java === | + | == Example: Configure an XMLDirectMapping == |
+ | |||
+ | The following example will demonstrate how to configure an XMLDirectMapping via XML Metadata: | ||
+ | |||
+ | === org.example.Employee.java === | ||
<source lang="java"> | <source lang="java"> | ||
Line 20: | Line 24: | ||
public String projectName; | public String projectName; | ||
} | } | ||
− | </source> | + | </source> |
− | === org.example.EmployeeProject.java (project class snippet) === | + | === org.example.EmployeeProject.java (project class snippet) === |
<source lang="java"> | <source lang="java"> | ||
Line 61: | Line 65: | ||
descriptor.addMapping(mgrIdMapping); | descriptor.addMapping(mgrIdMapping); | ||
descriptor.addMapping(projectNameMapping); | descriptor.addMapping(projectNameMapping); | ||
− | </source> | + | </source> |
− | === Deployment XML === | + | === Deployment XML === |
<source lang="xml"> | <source lang="xml"> | ||
Line 104: | Line 108: | ||
</namespace-resolver> | </namespace-resolver> | ||
</class-mapping-descriptor> | </class-mapping-descriptor> | ||
− | </source> | + | </source> |
− | === XML Metadata === | + | === XML Metadata === |
<source lang="xml"> | <source lang="xml"> | ||
Line 127: | Line 131: | ||
</java-types> | </java-types> | ||
</xml-bindings> | </xml-bindings> | ||
− | </source> | + | </source> |
− | === XML Instance Document === | + | === XML Instance Document === |
<source lang="xml"> | <source lang="xml"> |
Revision as of 17:16, 2 February 2010
XMLDirectMapping
Provide support for XML direct mappings via xml-element and xml-attribute.
Open Issues
- XPath - should we overload the 'name' attribute on xml-attribute and xml-element to set the XPath, or create a new attribute 'xml-path'?
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-attribute java-attribute="mgrId" xml-path="prj:project/@managerId" /> <xml-element java-attribute="firstName" xml-path="name/first-name/text()" /> <xml-element java-attribute="lastName" xml-path="name/last-name/text()" /> <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"> <name> <first-name>Joe</first-name> <last-name>Black</last-name> </name> <prj:project managerId="99">XML External Metadata Support</prj:project> </employee>