XJC生成整数而不是int [英] XJC Generating Integer Instead of int

查看:100
本文介绍了XJC生成整数而不是int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下架构应该在 Value 类中生成两个原始 int 字段,而是生成一个原语 int 用于元素属性的/Integer.htmlrel =noreferrer> java.lang.Integer

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>

我看了 JAXB文档,用于表示属性和元素可能以不同方式生成且未找到任何内容的任何内容。

I've looked through the JAXB documentation for anything that says that attributes and elements may be generated differently and found nothing.

任何人都可以解释这个?是否有修复使属性生成为原始 int

Can anyone explain this? Is there a fix to make the attribute generate as a primitive int?

推荐答案

<我不完全确定这是答案,但在调试我的应用程序时我有一个顿悟。

I'm not entirely sure this is the answer, but I had an epiphany while debugging my app.

元素的默认多样性在XML模式中, 1..1(必需)其中属性的默认多重性为 0。 .1(可选)

The default multiplicity for an element in an XML schema is 1..1 (required) where as the default multiplicity for an attribute is 0..1 (optional).


  1. 所以,因为元素必需并且Java中的原语具有默认值(很可能是0),生成< xsd:element type =xsd:int/> ; 作为Java原语。

  1. 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.

由于属性可选它可能是 nillable ,这是使用原语无法实现的。 java.lang.Integer 对象因此允许 null ,因此生成< xsd:attribute type =xsd是有意义的:int/> 作为 java.lang .Integer

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 an Object and thus allowed to be null, so it makes sense to generate an <xsd:attribute type="xsd:int" /> as an java.lang.Integer.

如果您将属性设为必需< xsd:attribute type =xsd:intuse =required/> ),它将生成原始 INT 。我没有看到JAXB明确说明这一点的文档,但这并不意味着它不存在;也许我只是错过了它。

If you make an attribute be required (<xsd:attribute type="xsd:int" use="required" />), it will generate as a primitive int. 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.

这篇关于XJC生成整数而不是int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆