Skip to main content

@XmlElements and required=false

2 replies [Last post]
Joined: 2005-10-14


I'm using a @XmlElements annotation to map an abstract class. I would like to make the element optional so that the unmarshalling doesn't fail if the tag doesn't exist. I thought I might be able to put the required=false attribute on the @XmlElements annotation, but that doesn't work. Could someone steer me in the right direction?

Joshua Smith

The relevant code fragment:

@XmlElement(name="Car", type=Car.class),
@XmlElement(name="Helicoptor", type=Helicoptor.class)
public void setVehicles(Vehicle[] _vehicles) {
vehicles = _vehicles;

(where Vehicle is my abstract class and Car and Helicoptor are classes that extend Vehicle)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2005-10-14

Belay my last.

I think I'm misunderstanding the error that I got. I thought it was telling me that the XML element was required and it was failing the parsing because it wasn't there. After looking closer at the schema it's clear that the minimum is 0 which means it's optional like I want it to be.

I have a bunch of interfaces involved and I think that's complicating the matter. I'll clean things up a bit and hopefully that will simplify the problem some.

Joshua Smith

Joined: 2003-06-09

Hope this ( would be of any help.

Also see for a somewhat related topic.

Lastly, we try very hard to provide an error message that's actually useful. So if you find some error messages to be confusing, please let us know so that we can improve it.