XJC 生成整数而不是 int
以下架构应在 Value
类中生成两个原始 int
字段,但为 元素 生成一个原始 int
字段strong> 和 java.lang.Integer 属性。
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com/test" xmlns:test="http://www.example.com/test"
elementFormDefault="qualified">
<xsd:element name="values">
<xsd:complexType>
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="test:value" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="value">
<xsd:complexType>
<xsd:sequence>
<!-- Is generated as primitive int -->
<xsd:element name="element" type="xsd:int" />
</xsd:sequence>
<!-- Is generated as java.lang.Integer -->
<xsd:attribute name="attribute" type="xsd:int" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
我浏览了 JAXB 文档,查找任何说明属性和元素可能以不同方式生成的内容并发现没有什么。
谁能解释一下吗?是否有修复方法可以使属性生成为原始 int
?
The following schema should be generating two primitive int
fields in a Value
class, but instead generates a primitive int
for the element and java.lang.Integer for the attribute.
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com/test" xmlns:test="http://www.example.com/test"
elementFormDefault="qualified">
<xsd:element name="values">
<xsd:complexType>
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="test:value" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="value">
<xsd:complexType>
<xsd:sequence>
<!-- Is generated as primitive int -->
<xsd:element name="element" type="xsd:int" />
</xsd:sequence>
<!-- Is generated as java.lang.Integer -->
<xsd:attribute name="attribute" type="xsd:int" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
I've looked through the JAXB documentation for anything that says that attributes and elements may be generated differently and found nothing.
Can anyone explain this? Is there a fix to make the attribute generate as a primitive int
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不完全确定这就是答案,但我在调试我的应用程序时顿悟了。
XML 模式中元素的默认多重性是
1..1(必需)
,而属性的默认多重性是>0..1(可选)
。因此,由于 元素 是必需的,并且 Java 中的原语具有默认值(很可能为 0),因此生成
< 是有意义的。 xsd:element type="xsd:int" />
作为 Java 原语。由于属性是可选,因此它可能是
nillable
,而使用原语是不可能的。 java.lang.Integer 是一个对象 ,因此允许为
null
,因此生成
作为 java.lang.Integer。如果您将属性设置为必需 (
) ,它将生成为原始int
。我还没有看到 JAXB 的文档明确说明了这一点,但这并不意味着它不存在;也许我只是错过了。I'm not entirely sure this is the answer, but I had an epiphany while debugging my app.
The default multiplicity for an element in an XML schema is
1..1 (required)
where as the default multiplicity for an attribute is0..1 (optional)
.So, since the element is required and a primitive in Java has a default value (most likely 0), it makes sense to generate an
<xsd:element type="xsd:int" />
as a Java primitive.Since the attribute is optional there is a possibility that it may be
nillable
which would not be possible using a primitive. The java.lang.Integer is anObject
and thus allowed to benull
, so it makes sense to generate an<xsd:attribute type="xsd:int" />
as an java.lang.Integer.If you make an attribute be required (
<xsd:attribute type="xsd:int" use="required" />
), it will generate as a primitiveint
. I haven't seen documentation by JAXB that explicitly says this, but that doesn't mean it doesn't exist; perhaps I just missed it.