bemol
bemol

Reputation: 105

How to add another attribute to xsi:nil element?

I have problem generating xml(gml) by marshalling classes generated from xsd(I'm using maven2 xjc plugin).

Problem occurs with elements which have gml:nilReason attribute which looks like this:

               <element name="foo" nillable="true" maxOccurs="unbounded">
                    <complexType>
                        <complexContent>
                            <extension base="string">
                                <attribute name="nilReason" type="gml:NilReasonType"/>
                            </extension>
                        </complexContent>
                    </complexType>
                </element>

The generated class looks like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class Foo
    extends String
{

    @XmlAttribute
    protected List<String> nilReason;

    /**
     * Gets the value of the nilReason property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the nilReason property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getNilReason().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link String }
     * 
     * 
     */
    public List<String> getNilReason() {
        if (nilReason == null) {
            nilReason = new ArrayList<String>();
        }
        return this.nilReason;
    }

    public boolean isSetNilReason() {
        return ((this.nilReason!= null)&&(!this.nilReason.isEmpty()));
    }

    public void unsetNilReason() {
        this.nilReason = null;
    }

    public boolean isNil() {
        return nil;
    }

    public void setNil(boolean nil) {
        this.nil = nil;
    }

}

Problem is that the attribute nilReason should be set together with xsi:nil. If I create instance of Foo and add to nilReason list value ex. "template" it wouldn't have xsi:nil attribute set to true. If I add null object to list i will get xsi:nil="true" but i can't set nilReason value.

I tried to look for some binding methods but with no luck.

The only solution i have found is to manually add attribute to the generated class like this:

@XmlAttribute(namespace = "http://www.w3.org/2001/XMLSchema-instance")
    private boolean nil;

but i really like to avoid interfering into generated classes.

Any ideas?

Upvotes: 3

Views: 875

Answers (0)

Related Questions