[英]JAXB: Bind element type xs:string to an Enum
My vendor has provided me with an XSD
that has an element defined as a String
. 我的供应商为我提供了一个
XSD
,它的元素定义为String
。 However in his spec, he indicates that this string can only have 1 of 3 values: 但是在他的说明中,他指出此字符串只能具有3个值中的1个:
Ex: 例如:
<xs:complexType name="MitigationStatus">
<xs:annotation>
<xs:documentation>
This represents a list of mitigation statuses. Can be OPEN, CLOSED, INVALID
</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="status" minOccurs="0" maxOccurs="unbounded" type="xs:string"/>
</xs:sequence>
</xs:complexType>
I would like to add something to my bindings file such that when I unmarshal my XML
, the status is not a String
but rather an Enum
. 我想在绑定文件中添加一些内容,以便在解组
XML
,状态不是String
而是Enum
。
All the docs I have found seem to indicate that the XSD
requires the String
to be of an enum
type. 我发现的所有文档似乎都表明
XSD
要求String
为enum
类型。
Is there a way to bind the string to an Enum
type even if the XSD
is not an enum
? 即使
XSD
不是enum
有没有办法将字符串绑定到Enum
类型?
You can define this restriction in the XML schema slightly modified with the changes indicated below and generate your Java classes from it. 您可以在XML模式中定义此限制,并通过以下指示的更改对其进行稍微修改,然后从中生成Java类。
<xs:simpleType name="StatusType">
<xs:restriction base="xs:string">
<xs:enumeration value="OPEN"/>
<xs:enumeration value="CLOSED"/>
<xs:enumeration value="INVALID"/>
</xs:restriction>
</xs:simpleType>
...
<xs:element name="status" minOccurs="0" maxOccurs="unbounded" type="StatusType"/>
...
This will result in an enum class StatusType. 这将导致枚举类StatusType。 In XML, everything is string anyway, so it remains as is.
在XML中,无论如何一切都是字符串,因此它保持原样。
If you really want to do this via a bindings file, you can use something like this: 如果您确实想通过绑定文件来执行此操作,则可以使用以下内容:
<jaxb:bindings node="//xsd:complexType[@name='MitigationStatus']">
<jaxb:bindings node="./xsd:element[@name='status']">
<jaxb:property>
<jaxb:baseType>
<jaxb:javaType name="StatusImpl"
parseMethod="StatusImpl.parseStringToEnum"
printMethod="StatusImpl.printEnumToString"/>
</jaxb:baseType>
</jaxb:property>
</jaxb:bindings>
</jaxb:bindings>
Assuming an enum StatusType, class StatusImpl is simply 假设一个枚举StatusType,类StatusImpl就是
public class StatusImpl {
public static StatusType parseStringToEnum( String value ){
return StatusType.valueOf( value );
}
public static String printEnumToString( StatusType impl ){
return impl.toString();
}
}
I don't know what problem you are having, but the following works fine. 我不知道您遇到什么问题,但是以下方法可以正常工作。 As you can see, there's no XSD, just manually coded JAXB classes.
如您所见,没有XSD,只有手动编码的JAXB类。
public class Test {
public static void main(String[] args) throws Exception {
String xml = "<Path>\r\n" +
" <Direction>North</Direction>\r\n" +
" <Direction>North</Direction>\r\n" +
" <Direction>East</Direction>\r\n" +
" <Direction>South</Direction>\r\n" +
"</Path>";
JAXBContext jaxbContext = JAXBContext.newInstance(Path.class);
Path path = (Path)jaxbContext.createUnmarshaller().unmarshal(new StringReader(xml));
for (Direction dir : path.directions)
System.out.println(dir);
}
}
@XmlEnum
enum Direction {
@XmlEnumValue("North") NORTH,
@XmlEnumValue("South") SOUTH,
@XmlEnumValue("East") EAST,
@XmlEnumValue("West") WEST
}
@XmlRootElement(name = "Path")
class Path {
@XmlElement(name = "Direction")
List<Direction> directions = new ArrayList<>();
}
Output 输出量
NORTH
NORTH
EAST
SOUTH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.