繁体   English   中英

如何从java中的xsd生成Long类型变量

[英]How to generate Long class type variable from xsd in java

我正在使用xsd生成我的模型,以下是我的xsd文件之一,使用它我正在生成Generator模型,这里我的问题是我想要一个变量的类型是Long类类型但我得到long premitive数据类型。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="1.0">
<xsd:complexType name="Generator">
    <xsd:sequence>
        <xsd:element name="id" type="xsd:string" minOccurs="1" />
        <xsd:element name="name" type="xsd:string" minOccurs="1" />
        <xsd:element name="age" type="xsd:int" minOccurs="1" />
        <xsd:element name="timestamp" type="xsd:long" minOccurs="1" />
   </xsd:sequence>
</xsd:complexType>

生成的类如下: -

public class Generator
implements Cloneable, CopyTo, Equals, ToString
{

@XmlElement(required = true)
protected String id;
@XmlElement(required = true)
protected String name;
@XmlElement(required = true)
protected int age;
@XmlElement(required = true)
protected long timestamp;
// settter and getter methods
}

生成的timestamp值的类型为long但我希望它的类型为Long

几天前我遇到了类似的问题。 在玩了xsd之后,我发现了以下两点:

  1. 如果属性是必需的,那么xjc将使用基本类型生成,例如int,long
  2. 如果属性是可选的,那么您将拥有Object类型,例如Integer,Long等。

这是有道理的,因为必填字段需要具有默认值。 回到你的情况,如果你想timestamp是Long,那么删除minOccurs 希望能帮助到你。

我在XSD中也面临同样的问题,而我需要对象而不是原始对象。

对于您的情况,请尝试下面给出的选项4。 它应该工作,因为它也为我工作。

  1. type =“xs:long”minOccurs =“0”maxOccurs =“1”>(生成) - 长值(对象)

  2. type =“xs:long”minOccurs =“0”maxOccurs =“1”nillable =“true”>(生成) - JAXBElement值(对象)

  3. type =“xs:long”minOccurs =“1”maxOccurs =“1”>(生成) - 长值(原始)

  4. type =“xs:long” minOccurs =“1”maxOccurs =“1”nillable =“true”>(生成) - 长值 (对象)

我知道这是非常晚的回复,但它可能会帮助那些面临这些问题的人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM