Search code examples
javajibx

How to check in JIBX if element is nillable


I have xsd schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
  targetNamespace="urn:v1"
  xmlns="urn:v1"
  xmlns:reg="urn:v1"
  xmlns:xop="http://www.w3.org/2004/08/xop/include"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified"
  attributeFormDefault="unqualified"
  version="1.1.1">

  <xs:simpleType name="ItemType">    
    <xs:restriction base="xs:string">

    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="ListItemType">   
    <xs:list itemType="ItemType"/>
  </xs:simpleType>

  <xs:complexType name="Element">    
    <xs:sequence>
      <xs:element name="SubElement">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="SubSubElement" type="ListItemType" nillable="true" minOccurs="0"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>  
</xs:schema>

and java class generated by JIBX:

public class Element
{
    private java.util.List<String> subElements = new java.util.ArrayList<String>();

    /** 
     * Get the list of 'SubSubElement' element items.
     * 
     * @return list
     */
    public java.util.List<String> getSubElements() {
        return subElements;
    }

    /** 
     * Set the list of 'SubSubElement' element items.
     * 
     * @param list
     */
    public void setSubElements(java.util.List<String> list) {
        subElements = list;
    }

    /** 
     * Serializer for 'SubSubElement' element list.
     * 
     * @param values
     * @return text
     */
    public static String serializeSubElements(java.util.List<String> values) {
        if (values == null) {
            return null;
        } else {
            java.lang.StringBuffer buff = new java.lang.StringBuffer();
            for (java.util.Iterator<String> iter = values.iterator(); iter
                    .hasNext();) {
                if (buff.length() > 0) {
                    buff.append(' ');
                }
                String value = iter.next();
                buff.append(value);
            }
            return buff.toString();
        }
    }

    /** 
     * Deserializer for 'SubSubElement' element list.
     * 
     * @param text
     * @return values
     * @throws org.jibx.runtime.JiBXException on conversion error
     */
    public static java.util.List<String> deserializeSubElements(String text)
            throws org.jibx.runtime.JiBXException {
        org.jibx.runtime.IListItemDeserializer ldser = new org.jibx.runtime.IListItemDeserializer() {
            public java.lang.Object deserialize(String text) {
                return text;
            }
        };
        return (java.util.List<String>) org.jibx.runtime.Utility
                .deserializeList(text, ldser);
    }
}

how I can check if element is nillable ? which I have in xsd: nillable="true"

UPDATE:

ok some esier xsd without list:

<xs:schema targetNamespace="urn:v1" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.1.1" xmlns="urn:v1" xmlns:reg="urn:v1" xmlns:xop="http://www.w3.org/2004/08/xop/include" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:complexType name="Element">
         <xs:sequence minOccurs="0">
            <xs:element name="SubElement" type="type1" nillable="true" minOccurs="0"/>
         </xs:sequence>
   </xs:complexType>

    <xs:simpleType name="type1">        
        <xs:restriction base="xs:string" />
    </xs:simpleType>

</xs:schema>


public class Element
{
    private String subElement;

    /** 
     * Get the 'SubElement' element value.
     * 
     * @return value
     */
    public String getSubElement() {
        return subElement;
    }

    /** 
     * Set the 'SubElement' element value.
     * 
     * @param subElement
     */
    public void setSubElement(String subElement) {
        this.subElement = subElement;
    }
}

again no nillable


Solution

  • You have here xs:element name="SubSubElement" type="ListItemType" nillable="true" minOccurs="0" that translates to java.util.List<String> getSubElements().

    Every null value in resulting list would correspond to <SubSubElement xsi:nil="true"/> in xml data.

    There is no other practical way for nulls to appear in the list.

    So it seems to be enough just to check list items for null in this case.

    See http://www.ibm.com/developerworks/xml/library/ws-tip-null/index.html#N10142.

    Update

    JiBX docs state

    The xsi:nil attribute, also used in instance documents, was not supported by JiBX 1.0. JiBX 1.1 added support for this feature using the nillable attribute in the object attribute group.

    and then

    This attribute can only be used with objects that are bound to an element name.

    So the desired check is definitely not possible at all with mapping to java.util.List<String> getSubElements().

    You could change mapping to java.util.List<SubElement> getSubElements(). <SubSubElement/> would map to empty SubElement instance then.

    Quick full-text search through all JiBX sources didn't show up any notion for nil check possibility. It seems JiBX support for nillable is limited only to special marshalling rules. It makes sense assuming nillable is supported only for element-bound mappings.

    Example xsd

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema
      targetNamespace="urn:v1"
      xmlns="urn:v1"
      xmlns:reg="urn:v1"
      xmlns:xop="http://www.w3.org/2004/08/xop/include"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      elementFormDefault="qualified"
      attributeFormDefault="unqualified"
      version="1.1.1">
    
      <xs:complexType name="SubElement">    
        <xs:simpleContent>
          <xs:extension base="xs:string">
          </xs:extension>
        </xs:simpleContent>    
      </xs:complexType>
      <xs:simpleType name="SubElementList">   
        <xs:list itemType="SubElement"/>
      </xs:simpleType>
      <xs:complexType name="Element">    
        <xs:sequence>
          <xs:element name="SubElement">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="SubSubElement" type="SubElementList" nillable="true" minOccurs="0"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>  
    </xs:schema>