我正在使用下面列出的 XSD 和相应的 XML。一切都运转良好动态MOXy http://wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic但我不知道如何访问java中的枚举类型。
有什么建议么?
感谢帮助。
<?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");
要将枚举值用于 set() 操作,您需要首先使用 DynamicJAXBContext.getEnumConstant() 查找枚举常量,然后将其用于集合。例如:
Object NORTH = ctx.getEnumConstant("your.package.CompassDirection", "NORTH");
person.set("quadrant", NORTH);
要获取该值,您将调用正确的代码,但返回的值不会是字符串,而是与该字符串关联的实际枚举值对象。你应该使用:
Object quadrant = person.get("quadrant");
希望这可以帮助,
Rick
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)