java XJC限制将双精度转换为字符串
我通过从XSD生成带有javax.xml.bind
注释的类来读取XML
<xsd:complexType name="foo">
<xsd:attribute name="bar" type="xsd:double" />
</xsd:complexType>
因此,我的类生成为:
public Double getBar(){....}
好的,为了确保双属性为正,我使用xsd:restriction
<xsd:simpleType name="DoublePositive">
<xsd:restriction base="xsd:double">
<xsd:minExclusive value="0" />
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="foo">
<xsd:attribute name="bar" type="DoublePositive" />
</xsd:complexType>
太糟糕了,xjc生成了一个字符串而不是一个双精度字符串
public String getBar(){....}
我可以强制使用Double而不是String吗
解决方案:
我的问题错了。我有xsi:type="DoublePositive"
而不是type="DoublePositive"
# 1 楼答案
这是我的配置,我用它来生成类
测试。xsd-您的xsd文件
文本。xml-具有全局绑定的文件
转换器。带有转换函数的java类
就这些。使用命令调用xjc
xjc -b test.xml -classpath . test.xsd
和生成的文件内容:
和适配器类
因此,正如您所看到的,xjc正确地生成了类。如果您仍然得到一个
String
,那么可能您忘记了在配置中添加一些内容。但是如果没有关于配置的详细信息,就不可能说出代码的错误