Search code examples
javaandroidxmlsimple-framework

Simple XML: ValueRequiredException


I'm stuck: I see no error in my Code but getting a ValueRequiredException every time I deserialize my XML:

Unable to satisfy @org.simpleframework.xml.Attribute(empty=, name=epc_memory, required=true) on field 'epcMemory' protected byte[] de.sysvision.aviocheck.xml.generated.Object.epcMemory for class de.sysvision.aviocheck.xml.generated.Object at line 14

I'm Using this XML:

    <?xml version="1.0" encoding="utf-8" ?>
    <avio_check_inventory_data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                               xsi:noNamespaceSchemaLocation="AvioCheckInventoryData.xsd" 
                               version="2.0">
      <avio_check_meta_data>
        <original_timestamp>20130412152652000</original_timestamp>
        <last_changed_timestamp>20130412152652000</last_changed_timestamp>
        <content_type>master</content_type>
        <ac_reg>D-AGWA</ac_reg>
      </avio_check_meta_data> 
      <avio_check_payload>
          <object epc_memory="3B3A00F1C30E008F4550430C30C30C40">    
            <type>LV</type>                 
            <type_id>PAX</type_id>                                      
            <layer> <id>1</id> <region_id>A1</region_id> </layer>
            <layer> <id>2</id> <region_id>A1</region_id> </layer>   
            <property id="MFR">78673</property>                 
            <property id="PNO">S51150-6300</property>                   
            <property id="DMF">20111101</property>                  
            <property id="EXP">20161101</property>                  
            <property id="LAC">1001A</property> 
          </object>                 
      </avio_check_payload>
    </avio_check_inventory_data>

This is my Simple XML Object class:

    @Root(name = "object")
    public class Object
    {
      @Attribute(required = false)
      protected String noNamespaceSchemaLocation;

      @Element(required = true)
      protected String type;
      @Element(name = "type_id", required = true)
      protected String typeId;
      protected List<Layer> layer;
      protected List<Property> property;
      @Attribute(name = "epc_memory", required = true)
      protected byte[] epcMemory;

      public String getType() {
        return type;
      }

      public void setType(String value) {
        this.type = value;
      }

      public String getTypeId() {
        return typeId;
      }

      public void setTypeId(String value) {
        this.typeId = value;
      }

      public List<Layer> getLayer() {
        if (layer == null) {
            layer = new ArrayList<Layer>();
        }
        return this.layer;
      }

      public List<Property> getProperty() {
        if (property == null) {
            property = new ArrayList<Property>();
        }
        return this.property;
      }

      public byte[] getEpcMemory() {
        return epcMemory;
      }

      public void setEpcMemory(byte[] value) {
        this.epcMemory = ((byte[]) value);
      }
    }

If I set "required" from epcMemory to false, then this works! The new Exception is:

Unable to satisfy @org.simpleframework.xml.Element(data=false, name=, required=true, type=void) on field 'type' protected java.lang.String de.sysvision.aviocheck.xml.generated.Object.type for class de.sysvision.aviocheck.xml.generated.Object at line 14

Can anybody help and ans show me what I'm doing wrong?

This is the corresponding XML-schema:

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
      <xs:simpleType name="DateTimeYYYYMMDDHHMMSSmmm">
        <xs:restriction base="xs:string">
          <xs:pattern value="\d{17}"/>
        </xs:restriction>
      </xs:simpleType>

      <xs:simpleType name="content_type">
        <xs:list>
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:pattern value="master"/>
              <xs:pattern value="update"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:list>
      </xs:simpleType>

      <xs:element name="avio_check_inventory_data">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="avio_check_meta_data" minOccurs="1" maxOccurs="1">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="original_timestamp" type="DateTimeYYYYMMDDHHMMSSmmm" minOccurs="1" maxOccurs="1" />
                  <xs:element name="last_changed_timestamp" type="DateTimeYYYYMMDDHHMMSSmmm" minOccurs="0" maxOccurs="1"/>
                  <xs:element name="content_type" type="content_type" minOccurs="1" maxOccurs="1"/>
                  <xs:element name="ac_reg" type="xs:string" minOccurs="1" maxOccurs="1" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
            <xs:element name="avio_check_payload" minOccurs="1" maxOccurs="1">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="object" minOccurs="1" maxOccurs="unbounded">
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element name="type" type="xs:string" minOccurs="1" maxOccurs="1" />
                        <xs:element name="type_id" type="xs:string" minOccurs="1" maxOccurs="1" />
                        <xs:element name="layer" minOccurs="0" maxOccurs="unbounded">
                          <xs:complexType>                        
                            <xs:sequence>
                              <xs:element name="id" type="xs:string" minOccurs="1" maxOccurs="1"/>
                              <xs:element name="region_id" type="xs:string" minOccurs="1" maxOccurs="1">
                                <xs:annotation>
                                  <xs:documentation xml:lang="en">
                                    This is the same id as in used layout.xml!
                                  </xs:documentation>
                                </xs:annotation>
                              </xs:element>
                            </xs:sequence>
                           </xs:complexType>
                        </xs:element>                  
                        <xs:element name="property" minOccurs="0" maxOccurs="unbounded">
                          <xs:complexType>
                            <xs:simpleContent>
                              <xs:extension base="xs:string">
                                <xs:attribute name="id" type="xs:string" />
                              </xs:extension>
                            </xs:simpleContent>
                          </xs:complexType>
                        </xs:element>
                      </xs:sequence>
                      <xs:attribute name="epc_memory" type="xs:hexBinary" use="required" />
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
          <xs:attribute name="version" type="xs:decimal" use="required"/>
        </xs:complexType>
      </xs:element>
    </xs:schema>

Solution

  • The Answer was simple: I had to use the ElementList annotation with inline=true

    @ElementList(inline = true)

        package de.sysvision.aviocheck.xml.generated;
    
        import java.util.List;
    
        import org.simpleframework.xml.Attribute;
        import org.simpleframework.xml.Element;
        import org.simpleframework.xml.ElementList;
        import org.simpleframework.xml.Root;
    
        @Root(strict = false)
        public class Object
        {
            @Attribute(name = "epc_memory")
            private String epcMemory;
    
            @Element(required = false)
            private String type;
    
            @Element(name = "type_id")
            private String typeId;
    
            @ElementList(inline = true)
            private List<Layer> layers;
    
            @ElementList(inline = true)
            private List<Property> properties;
    
            public String getEpcMemory() {
                return epcMemory;
            }
    
            public void setEpcMemory(String epcMemory) {
                this.epcMemory = epcMemory;
            }
    
            public String getType() {
                return type;
            }
    
            public void setType(String type) {
                this.type = type;
            }
    
            public String getTypeId() {
                return typeId;
            }
    
            public void setTypeId(String typeId) {
                this.typeId = typeId;
            }
    
            public List<Layer> getLayers() {
                return layers;
            }
    
            public void setLayers(List<Layer> layers) {
                this.layers = layers;
            }
    
            public List<Property> getProperties() {
                return properties;
            }
    
            public void setProperties(List<Property> properties) {
                this.properties = properties;
            }
    
        }