users@jaxb.java.net

JAXB and nillable="true" error

From: juan.velez <juan.velez_at_areva-td.com>
Date: Sat, 10 Jan 2009 14:14:21 -0800 (PST)

I do not know if this is a bug, but I think it is. I have the following XML
Schema

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
xmlns:tns="http://schemas.datacontract.org/2004/07/Areva.Csm.Service.DC"
           elementFormDefault="qualified"
          
targetNamespace="http://schemas.datacontract.org/2004/07/Areva.Csm.Service.DC"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="UpdateImportExportRequestTargetStatus">
    <xs:sequence>
      <xs:element name="RequestId" type="xs:decimal" />
      <xs:element minOccurs="0" name="SequenceId" nillable="true"
type="xs:string" />
      <xs:element minOccurs="0" name="TargetId" nillable="true"
type="xs:string" />
      <xs:element minOccurs="0" name="TargetStatus"
type="tns:ImportExportTargetStatus" />
      <xs:element minOccurs="0" name="TypeId" nillable="true"
type="xs:string" />
    </xs:sequence>
  </xs:complexType>
  <xs:element name="UpdateImportExportRequestTargetStatus" nillable="true"
type="tns:UpdateImportExportRequestTargetStatus" />
  <xs:simpleType name="ImportExportTargetStatus">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Successful" />
      <xs:enumeration value="Failed" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

I run the JAXB generation code and I end up with the following
UpdateImportExportRequestTargetStatus class

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB)
Reference Implementation, vhudson-jaxb-ri-2.1-646
// See http://java.sun.com/xml/jaxb http://java.sun.com/xml/jaxb
// Any modifications to this file will be lost upon recompilation of the
source schema.
// Generated on: 2009.01.10 at 01:55:39 PM PST
//


package com.areva.csm.service.schema;

import java.math.BigDecimal;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for UpdateImportExportRequestTargetStatus complex type.
 *
 * <p>The following schema fragment specifies the expected content contained
within this class.
 *
 * <pre>
 * &lt;complexType name="UpdateImportExportRequestTargetStatus">
 * &lt;complexContent>
 * &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 * &lt;sequence>
 * &lt;element name="RequestId"
type="{http://www.w3.org/2001/XMLSchema}decimal"/>
 * &lt;element name="SequenceId"
type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 * &lt;element name="TargetId"
type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 * &lt;element name="TargetStatus"
type="{http://schemas.datacontract.org/2004/07/Areva.Csm.Service.DC}ImportExportTargetStatus"
minOccurs="0"/>
 * &lt;element name="TypeId"
type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 * &lt;/sequence>
 * &lt;/restriction>
 * &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "UpdateImportExportRequestTargetStatus", propOrder = {
    "requestId",
    "sequenceId",
    "targetId",
    "targetStatus",
    "typeId"
})
public class UpdateImportExportRequestTargetStatus {

    @XmlElement(name = "RequestId", required = true)
    protected BigDecimal requestId;
    @XmlElementRef(name = "SequenceId", namespace =
"http://schemas.datacontract.org/2004/07/Areva.Csm.Service.DC", type =
JAXBElement.class)
    protected JAXBElement<String> sequenceId;
    @XmlElementRef(name = "TargetId", namespace =
"http://schemas.datacontract.org/2004/07/Areva.Csm.Service.DC", type =
JAXBElement.class)
    protected JAXBElement<String> targetId;
    @XmlElement(name = "TargetStatus")
    protected ImportExportTargetStatus targetStatus;
    @XmlElementRef(name = "TypeId", namespace =
"http://schemas.datacontract.org/2004/07/Areva.Csm.Service.DC", type =
JAXBElement.class)
    protected JAXBElement<String> typeId;

    /**
     * Gets the value of the requestId property.
     *
     * @return
     * possible object is
     * {_at_link BigDecimal }
     *
     */
    public BigDecimal getRequestId() {
        return requestId;
    }

    /**
     * Sets the value of the requestId property.
     *
     * @param value
     * allowed object is
     * {_at_link BigDecimal }
     *
     */
    public void setRequestId(BigDecimal value) {
        this.requestId = value;
    }

    /**
     * Gets the value of the sequenceId property.
     *
     * @return
     * possible object is
     * {_at_link JAXBElement }{_at_code <}{_at_link String }{_at_code >}
     *
     */
    public JAXBElement<String> getSequenceId() {
        return sequenceId;
    }

    /**
     * Sets the value of the sequenceId property.
     *
     * @param value
     * allowed object is
     * {_at_link JAXBElement }{_at_code <}{_at_link String }{_at_code >}
     *
     */
    public void setSequenceId(JAXBElement<String> value) {
        this.sequenceId = ((JAXBElement<String> ) value);
    }

    /**
     * Gets the value of the targetId property.
     *
     * @return
     * possible object is
     * {_at_link JAXBElement }{_at_code <}{_at_link String }{_at_code >}
     *
     */
    public JAXBElement<String> getTargetId() {
        return targetId;
    }

    /**
     * Sets the value of the targetId property.
     *
     * @param value
     * allowed object is
     * {_at_link JAXBElement }{_at_code <}{_at_link String }{_at_code >}
     *
     */
    public void setTargetId(JAXBElement<String> value) {
        this.targetId = ((JAXBElement<String> ) value);
    }

    /**
     * Gets the value of the targetStatus property.
     *
     * @return
     * possible object is
     * {_at_link ImportExportTargetStatus }
     *
     */
    public ImportExportTargetStatus getTargetStatus() {
        return targetStatus;
    }

    /**
     * Sets the value of the targetStatus property.
     *
     * @param value
     * allowed object is
     * {_at_link ImportExportTargetStatus }
     *
     */
    public void setTargetStatus(ImportExportTargetStatus value) {
        this.targetStatus = value;
    }

    /**
     * Gets the value of the typeId property.
     *
     * @return
     * possible object is
     * {_at_link JAXBElement }{_at_code <}{_at_link String }{_at_code >}
     *
     */
    public JAXBElement<String> getTypeId() {
        return typeId;
    }

    /**
     * Sets the value of the typeId property.
     *
     * @param value
     * allowed object is
     * {_at_link JAXBElement }{_at_code <}{_at_link String }{_at_code >}
     *
     */
    public void setTypeId(JAXBElement<String> value) {
        this.typeId = ((JAXBElement<String> ) value);
    }

}

However, when I try to unmarshall (with schema validation) a valid XML
fragment

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:UpdateImportExportRequestTargetStatus
       
xmlns:ns2="http://schemas.datacontract.org/2004/07/Areva.Csm.Service.DC"
        xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
  <ns2:RequestId>123</ns2:RequestId>
  <ns2:SequenceId xs:nil="true" />
  <ns2:TargetId xs:nil="true" />
  <ns2:TargetStatus xs:nil="true" />
  <ns2:TypeId xs:nil="true" />
</ns2:request>

I get the following which I think it must be a JAXB bug

Exception in thread "main" javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException: cvc-elt.3.1: Attribute
'http://www.w3.org/2001/XMLSchema-instance,nil' must not appear on element
'ns2:TargetStatus', because the {nillable} property of 'ns2:TargetStatus' is
false.]
        at
javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
        at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:510)
        at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:215)
        at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
        at
javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
        at
javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)


The code to unmarshall is:

        SchemaFactory schemaFactory =
           
SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);

        List<InputStream> inputStreams = new ArrayList<InputStream>();
       
inputStreams.add(ClassLoader.getSystemResourceAsStream("Areva.Csm.Service.DC.xsd"));

        List<Source> sources = new ArrayList<Source>();
        for (InputStream is: inputStreams) {
            sources.add(new StreamSource(is));
        }

        Source[] schemaSources = new Source[sources.size()];
        sources.toArray(schemaSources);
        Schema schema = null;
        schema = schemaFactory.newSchema(schemaSources);

        final StringBuffer request = new StringBuffer(
            "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
+
            "<ns2:UpdateImportExportRequestTargetStatus
xmlns:ns2=\"http://schemas.datacontract.org/2004/07/Areva.Csm.Service.DC\" "
+
            "
xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\">" +
            " <ns2:RequestId>123</ns2:RequestId>" +
            " <ns2:SequenceId xs:nil=\"true\" />"+
            " <ns2:TargetId xs:nil=\"true\" />"+
            " <ns2:TargetStatus xs:nil=\"true\" />"+
            " <ns2:TypeId xs:nil=\"true\" />"+
            "</ns2:request>");

        JAXBContext jc;
        jc = JAXBContext.newInstance( "com.areva.csm.service.schema" );
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setSchema(schema);

        JAXBElement<?> element2 = (JAXBElement<?>)
unmarshaller.unmarshal(new ByteArrayInputStream(
            request.toString().getBytes()));
        UpdateImportExportRequestTargetStatus status =
            (UpdateImportExportRequestTargetStatus) element2.getValue();

BTW I am using Java 1.5 and JAXB-RI 2.1.7




-- 
View this message in context: http://www.nabble.com/JAXB-and-nillable%3D%22true%22-error-tp21393490p21393490.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.