EclipseLink动态MOXy访问枚举值 [英] EclipseLink dynamic MOXy accessing enum values

查看:133
本文介绍了EclipseLink动态MOXy访问枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面列出的XSD和相应的XML。一切都适用于动态MOXy ,但我不知道如何访问java中的枚举类型。
任何建议?
感谢您的帮助。

 <?xml version =1.0encoding =UTF-8?> ; 
< xs:schema ...>
< xs:element name =person>
< xs:complexType>
< xs:sequence>
< xs:element name =first-nametype =xs:string/>
< xs:element name =last-nametype =xs:string/>
< xs:element name =quadranttype =myns:compass-direction/>
< / xs:sequence>
< / xs:complexType>
< / xs:element>

< xs:simpleType name =compass-direction>
< xs:restriction base =xs:string>
< xs:枚举值=NORTH/>
< xs:枚举值=SOUTH/>
< xs:枚举值=EAST/>
< xs:枚举值=WEST/>
< / xs:restriction>
< / xs:simpleType>

< / xs:schema>

// JAVA代码
DynamicEntity person =(DynamicEntity)dynamicJAXBContext.createUnmarshaller()。unmarshal(instanceDoc);
String firstName = person.get(firstName);
String lastName = person.get(lastName);
//直到这里它工作得很好

//但现在:如何获取并设置象限的值?
//以下行不起作用
String quadrant = person.get(quadrant);
person.set(quadrant,NORTH);


解决方案

要使用枚举值进行set()操作,您需要先查找枚举常量使用DynamicJAXBContext.getEnumConstant(),然后使用该集合。例如:

 对象NORTH = ctx。 getEnumConstant(your.package.CompassDirection,NORTH); 
person.set(quadrant,NORTH);

要获取该值,您正在调用正确的代码,但返回的值不会是一个String,它将与该String相关联的实际枚举值对象应该使用: / p>

 对象象限= person.get(象限); 

希望这有帮助,



Rick


I'm using the XSD listed below and a corresponding XML. Everything works well with dynamic MOXy but I haven't 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().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");

解决方案

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

这篇关于EclipseLink动态MOXy访问枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆