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
(→Java Code) |
|||
Line 16: | Line 16: | ||
|} | |} | ||
− | == Example: Configure an XMLDirectMapping | + | == Example: Configure an XMLDirectMapping == |
− | The following example will demonstrate how to configure an XMLDirectMapping via | + | The following example will demonstrate how to configure an XMLDirectMapping via XML Metadata: |
=== Java Code === | === Java Code === | ||
Line 24: | Line 24: | ||
<source lang="java"> | <source lang="java"> | ||
XMLDirectMapping idMapping = new XMLDirectMapping(); | XMLDirectMapping idMapping = new XMLDirectMapping(); | ||
− | idMapping.setAttributeName(" | + | idMapping.setAttributeName("empId"); |
idMapping.setXPath("@id"); | idMapping.setXPath("@id"); | ||
XMLDirectMapping firstNameMapping = new XMLDirectMapping(); | XMLDirectMapping firstNameMapping = new XMLDirectMapping(); | ||
firstNameMapping.setAttributeName("firstName"); | firstNameMapping.setAttributeName("firstName"); | ||
− | firstNameMapping.setXPath("first | + | firstNameMapping.setXPath("name/first-name/text()"); |
XMLDirectMapping lastNameMapping = new XMLDirectMapping(); | XMLDirectMapping lastNameMapping = new XMLDirectMapping(); | ||
lastNameMapping.setAttributeName("lastName"); | lastNameMapping.setAttributeName("lastName"); | ||
− | lastNameMapping.setXPath("last | + | 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 namespaceResolver = new NamespaceResolver(); | ||
− | namespaceResolver. | + | namespaceResolver.setDefaultNamespaceURI("http://www.example.com/employees") |
− | namespaceResolver.put(" | + | namespaceResolver.put("prj", "http://www.example.com/projects"); |
− | + | ||
XMLDescriptor descriptor = new XMLDescriptor(); | XMLDescriptor descriptor = new XMLDescriptor(); | ||
descriptor.setJavaClass(Employee.class); | descriptor.setJavaClass(Employee.class); | ||
− | descriptor.setDefaultRootElement(" | + | descriptor.setDefaultRootElement("employee"); |
descriptor.setNamespaceResolver(namespaceResolver); | descriptor.setNamespaceResolver(namespaceResolver); | ||
+ | descriptor.addMapping(idMapping); | ||
descriptor.addMapping(firstNameMapping); | descriptor.addMapping(firstNameMapping); | ||
descriptor.addMapping(lastNameMapping); | descriptor.addMapping(lastNameMapping); | ||
+ | descriptor.addMapping(mgrIdMapping); | ||
+ | descriptor.addMapping(projectNameMapping); | ||
+ | </source> | ||
=== XML Metadata === | === XML Metadata === | ||
Line 51: | Line 62: | ||
<source lang="xml"> | <source lang="xml"> | ||
<?xml version="1.0" encoding="US-ASCII"?> | <?xml version="1.0" encoding="US-ASCII"?> | ||
− | <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/ | + | <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-types> | ||
− | <java-type name="org. | + | <java-type name="org.eclipse.persistence.testing.jaxb.externalizedmetadata.xmlpath.Employee"> |
+ | <xml-root-element name="employee" /> | ||
<java-attributes> | <java-attributes> | ||
− | <xml- | + | <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-attributes> | ||
</java-type> | </java-type> |
Revision as of 12:31, 2 February 2010
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>