MOXy mapping question [message #389791] |
Mon, 06 July 2009 15:51  |
Eclipse User |
|
|
|
Hi!
I'm having a bit of trouble mapping an object to XML using MOXy.
My object looks something like this:
public class DesignationRequest {
private String numericId;
private String alphanumericId;
// Getters and setters.
}
The thing is only one of the Ids should be set be set and the other should
be null. What's more is that each of the Ids come in two different
"flavours", meaning the XML can be one of these four variations:
<app:DesignationRequest>
<app:numericId>
<value>625</value>
</app:numericId>
</app:DesignationRequest>
or
<app:DesignationRequest>
<app:numericIdTypeB>
<value>625</value>
</app:numericIdTypeB>
</app:DesignationRequest>
or
<app:DesignationRequest>
<app:alphanumericId>
<value>62e</value>
</app:alphanumericId>
</app:DesignationRequest>
or
<app:DesignationRequest>
<app:alphanumericIdTypeB>
<value>62e</value>
</app:alphanumericIdTypeB>
</app:DesignationRequest>
The XSD ought to look something like the following, though I can't quite
seem to get the namespaces right:
<xs:element name="DesignationRequest">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element name="numericId" type="value"/>
<xs:element name="numericIdTypeB" type="value"/>
<xs:element name="alphanumericId" type="value"/>
<xs:element name="alphanumericIdTypeB" type="value"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="value">
<xs:sequence>
<xs:element name="value" type="xs:string"/>
</xs:sequence>
</xs:complexType>
Anyway, I have no idea how to do the MOXy mapping. Is
XMLChoiceObjectMapping the way to go, or can I use XMLDirectMapping
somehow? I've tried choice mapping by using
descriptor.addChoiceElement("app:numericId/value", String.class);
though this gives an error that the String class is not defined or
something something of the sort. Any help would be appreciated.
/Matti
|
|
|
Re: MOXy mapping question [message #389793 is a reply to message #389791] |
Mon, 06 July 2009 17:40   |
Eclipse User |
|
|
|
Hello Matti,
You could map this as follows. I believe you were just missing the
"/text()" at the end of your XPath statements on your choice mappings.
import org.eclipse.persistence.oxm.NamespaceResolver;
import org.eclipse.persistence.oxm.XMLDescriptor;
import org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping;
import org.eclipse.persistence.sessions.Project;
public class DesignationRequestProject extends Project {
public DesignationRequestProject() {
this.addDescriptor(getDesignationRequestDescriptor());
}
private XMLDescriptor getDesignationRequestDescriptor() {
XMLDescriptor xmlDescriptor = new XMLDescriptor();
xmlDescriptor.setJavaClass(DesignationRequest.class);
xmlDescriptor.setDefaultRootElement("app:DesignationRequest ");
NamespaceResolver namespaceResolver = new NamespaceResolver();
namespaceResolver.put("app", "urn:your-namespace");
xmlDescriptor.setNamespaceResolver(namespaceResolver);
XMLChoiceObjectMapping numericIdMapping = new
XMLChoiceObjectMapping();
numericIdMapping.setAttributeName("numericId");
numericIdMapping.addChoiceElement("app:numericId/value/text() ",
String.class);
numericIdMapping.addChoiceElement("app:numericIdTypeB/value/text() ",
String.class);
xmlDescriptor.addMapping(numericIdMapping);
XMLChoiceObjectMapping alphanumericIdMapping = new
XMLChoiceObjectMapping();
alphanumericIdMapping.setAttributeName("alphanumericId");
alphanumericIdMapping.addChoiceElement("app:alphanumericId/value/text() ",
String.class);
alphanumericIdMapping.addChoiceElement("app:alphanumericIdTypeB/value/text() ",
String.class);
xmlDescriptor.addMapping(alphanumericIdMapping);
return xmlDescriptor;
}
}
For your XML schema to get the namespace qualification from your examples
you could use something like the one below. Note you will need to use
elementFormDefault="unqualified" and make all elements that will be
namespace qualified global elements.
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified"
attributeFormDefault="unqualified"
targetNamespace="urn:your-namespace" xmlns="urn:your-namespace">
<xs:element name="DesignationRequest">
<xs:complexType>
<xs:choice>
<xs:element ref="numericId"/>
<xs:element ref="numericIdTypeB"/>
<xs:element ref="alphanumericId"/>
<xs:element ref="alphanumericIdTypeB"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="numericId" type="value"/>
<xs:element name="numericIdTypeB" type="value"/>
<xs:element name="alphanumericId" type="value"/>
<xs:element name="alphanumericIdTypeB" type="value"/>
<xs:complexType name="value">
<xs:sequence>
<xs:element name="value" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
-Blaise
Blaise Doughan
Team Lead, EclipseLink OXM/JAXB/SDO
|
|
|
|
Powered by
FUDForum. Page generated in 0.03571 seconds