Dynamic Moxy accessing enum values [message #657607] |
Thu, 03 March 2011 08:21  |
Eclipse User |
|
|
|
Hi there,
I'm using the xsd listed below and a corresponding xml. Everything works well with dynamic moxy
but I havent any idea how to access the enum type within java.
Any suggestions?
Thanks for help.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema ...>
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="first-name" type="xs:string"/>
<xs:element name="last-name" type="xs:string"/>
<xs:element name="quadrant" type="myns:compass-direction"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="compass-direction">
<xs:restriction base="xs:string">
<xs:enumeration value="NORTH"/>
<xs:enumeration value="SOUTH"/>
<xs:enumeration value="EAST"/>
<xs:enumeration value="WEST"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
//JAVA code
DynamicEntity person = (DynamicEntity)dynamicJAXBContext.createUnmarshaller().unmar shal( "person1.xml");
String firstName = person.get("firstName");
String lastName = person.get("lastName");
//until here it works well
//but now: how to get and set the value of the "quadrant"?
// following lines do not work
String quadrant=person.get("quadrant);
person.set("quadrant","NORTH");
|
|
|
Re: Dynamic Moxy accessing enum values [message #657939 is a reply to message #657607] |
Fri, 04 March 2011 11:57  |
Eclipse User |
|
|
|
I am posting the following on behalf of Rick Barkhouse, who originally answered this question on Stack Overflow ( http://stackoverflow.com/questions/5181374/eclipselink-dynam ic-moxy-accessing-enum-values/5183847#5183847):
---
Hi Micha,
To use an enum value for a set() operation, you need to first look up the enum constant using DynamicJAXBContext.getEnumConstant(), and then use that for the set. For example:
Object NORTH = ctx.getEnumConstant("your.package.CompassDirection", "NORTH");
person.set("quadrant", NORTH);
To get the value, you are calling the correct code, but the value that comes back will not be a String, it will the actual enum value Object associated with that String. You should use:
Object quadrant = person.get("quadrant");
Hope this helps,
Rick
|
|
|
Powered by
FUDForum. Page generated in 0.05177 seconds