EclipseLink dynamic MOXy access to enumeration values

I use the XSD listed below and the corresponding XML. Everything works well with dynamic MOXy , but I don't know how to access the enum type in java. Any suggestions? Thanks for the 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().unmarshal(instanceDoc);
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");
+3
source share
1 answer

To use the enum value for the set () operation, you first need to view the enum constant with DynamicJAXBContext.getEnumConstant (), and then use it for the set. For instance:

Object NORTH = ctx.getEnumConstant("your.package.CompassDirection", "NORTH");
person.set("quadrant", NORTH);

To get the value, you call the correct code, but the return value will not be a string, it will be the actual value of the Object enumeration associated with this string. You should use:

Object quadrant = person.get("quadrant");

, ,

Rick

+1

Source: https://habr.com/ru/post/1796041/


All Articles