xjc:覆盖xs:simpleType定义 [英] xjc: override xs:simpleType definition
问题描述
我正在使用 xjc
为Java类编译一组XSD。我希望能够覆盖给定简单类型的数据类型定义。 XSD片段是:
I am compiling a set of XSDs to Java classes using xjc
. I would like to be able to override the data type definition for a given simple type. The XSD snippet is:
<xs:simpleType name="CPT-DateTime">
<xs:annotation>
<xs:appinfo>Can be specified as a integer number or as xs:dateTime</xs:appinfo>
</xs:annotation>
<xs:union memberTypes="xs:unsignedLong xs:dateTime"/>
</xs:simpleType>
在 CPT-DateTime的元素中产生(不足为奇) / code>在生成的Java类中将类型定义为
String
,例如
public class CcReportTrainInitialization {
...
@XmlElement(required = true)
protected String time;
...
public String getTime() {
return time;
}
public void setTime(String value) {
this.time = value;
}
...
我想要的数据类型是 time
(在此示例中)是特定于日期时间的类型,例如 XMLGregorianCalendar
或类似的东西:
What I would like is for the datatype of time
(in this example) to be a date-time specific type, e.g. XMLGregorianCalendar
or something like that:
public class CcReportTrainInitialization {
...
@XmlElement(required = true)
protected XMLGregorianCalendar time;
...
public XMLGregorianCalendar getTime() {
return time;
}
public void setTime(XMLGregorianCalendar value) {
this.time = value;
}
...
这可能吗?
我一直在尝试使用绑定文件,但我不确定它是否可行。建议?
I've been experimenting with a binding file but I'm not sure it's even possible to do. Suggestions?
推荐答案
其他选项包括:
-
jaxb:baseType
-
jaxb:javaType
-
xjc:javaType
- 如jaxb:javaType
但允许指定适配器类而不是unmarshal
/marshal
方法。
jaxb:baseType
jaxb:javaType
xjc:javaType
- likejaxb:javaType
but allows specifying the adapter class instead ofunmarshal
/marshal
methods.
我实际上认为 jaxb:class / @ ref
自定义不适合简单类型,因为这使它成为类类型。这对于内部XJC模型很重要,一些XJC插件可能会错误地处理你的类型。
I would actually argue that jaxb:class/@ref
customization is not right for the simple type as this makes it a "class" type. This matters for the internal XJC model, some of the XJC plugins may handle your type incorrectly.
我认为你应该使用 jaxb:javaType
这里。尝试:
I think you should use jaxb:javaType
here. Try:
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings jxb:version="2.1" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="urn:your-target-namespace">
<jxb:bindings node="/xs:schema" schemaLocation="../TCIP_4_0_0_Final.xsd">
<jxb:globalBindings>
<jxb:javaType name="javax.xml.datatype.XMLGregorianCalendar" xmlType="tns:CPT-DateTime"/>
</jxb:globalBindings>
</jxb:bindings>
</jxb:bindings>
这篇关于xjc:覆盖xs:simpleType定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!