users@jaxb.java.net

Re: Nillable, minOccurs="0", maxOccurs="unbounded", JAXBElement necessary?

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Tue, 1 Jan 2013 09:48:02 +0100

On 01/01/2013, Slappy zd <slapnutcity_at_hotmail.com> wrote:
> If a null value is specified, how can I add an attribute with a specific
> value, "noValue"?

You don't have to add the attribute xsi:nil - it has to be handled by the
marshaller's XML writer.

> It is part of the specification I must follow on which the
> xsd is based, so I cannot change that. Just nil="true" is not allowed
> (functionally speaking).

The nil element still needs to be in the XML - otherwise you wouldn't be
aware of it at all. Hence it must contain xsi:nil="true". That's what the
XML schema requires. (Note the 3rd element, a null string, which is
something else.)

Here's an example:

<xs:element name="doc" type="DocType"/>
<xs:complexType name="DocType">
  <xs:sequence>
    <xs:element name="Title" type="xs:string" minOccurs="0"/>
    <xs:element name="Tipo" type="xs:string"
                minOccurs="0" maxOccurs="unbounded"
                nillable="true"/>
  </xs:sequence>
</xs:complexType>

        ObjectFactory of = new ObjectFactory();
        DocType doc = of.createDocType();
        JAXBElement<DocType> jbe = of.createDoc( doc );
        doc.setTitle( "The Title" );
        doc.getTipo().add( "Tipo 0" );
        doc.getTipo().add( null );
        doc.getTipo().add( "" );
        doc.getTipo().add( "Tipo 3" );

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doc>
    <Title>The Title</Title>
    <Tipo>Tipo 0</Tipo>
    <Tipo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <Tipo></Tipo>
    <Tipo>Tipo 3</Tipo>
</doc>




>
>> Date: Mon, 31 Dec 2012 17:02:15 +0100
>> Subject: Re: Nillable, minOccurs="0", maxOccurs="unbounded", JAXBElement
>> necessary?
>> From: wolfgang.laun_at_gmail.com
>> To: users_at_jaxb.java.net
>>
>> Since ArrayList allows null element there's no need for the more
>> elaborate
>> JAXBElement. A null List element should result in a
>> <foo xsi:nil="true"/>
>>
>> -W
>>
>>
>> On 31/12/2012, slapnutcity_at_hotmail.com <slapnutcity_at_hotmail.com> wrote:
>> > I've got an XSD (a part of a bigger XSD which I do not have control
>> > over), which is provided below.
>> >
>> > <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
>> > <xsd:element name="foo">
>> > <xsd:complexType>
>> > <xsd:sequence>
>> > <xsd:element name="bar" type="xsd:string"
>> > nillable="true" minOccurs="0" maxOccurs="unbounded"/>
>> > </xsd:sequence>
>> > </xsd:complexType>
>> > </xsd:element>
>> > </xsd:schema>
>> >
>> > I has an element "bar" which is of type "string", is nillable, does not
>> > have to occur, but it can also occur more than once. I've understood to
>> > make a difference between a nillable element and an element not
>> > provided, a JAXBElement is used.
>> >
>> > When I generate classes by using xjc, I get a class Foo with a
>> > List<String> bar.Isn't it supposed to be List<JAXBElement<String>> bar?
>> > If not, how can I make the difference between a value not provided and
>> > a nil value? When it is a nil value, it should be able to hold an
>> > attribute defining it has "noValue" (part of the specification I'm
>> > working with).
>> >
>