18

Does it mean the XML element is mandatory ? Or the XML element must have some non-null value ? I am really confused by the javadoc explanation.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
sateesh
  • 183
  • 1
  • 1
  • 5

2 Answers2

18
@XMLElement(required=true)

generates something like this in the XML schema:

<xs:element name="city" type="xs:string" minOccurs="1"/>

which means the element and a value are mandatory. The default is false.

This:

@XMLELement(nillable=true)

generates something like this in the XML schema:

<xs:element name="city" type="xs:string" nillable="true"/>

which means you can pass in a nil value in your XML like this:

<city xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

Combining the two like this:

@XMLELement(nillable=true, required=true)

gives an XML schema definition similar to this:

<xs:element name="city" type="xs:string" nillable="true"/>

which means the element is mandatory but you can pass in a nil value.

CodeClimber
  • 4,584
  • 8
  • 46
  • 55
5

If required() is true, then Javabean property is mapped to an XML schema element declaration with minOccurs="1"

The minOccurs indicator specifies the minimum number of times an element can occur. If element in the schema has minOccurs="1" attribute this means that the element is mandatory. It must appear in the XML document.

Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143
  • Thanks for taking time to dig into my question. I have clarified by creating a simple test harness. The mandatory constraint applies to both the element and its value. – sateesh Oct 08 '12 at 09:31