我被卡住了:我的代码中没有错误,但每次反序列化XML时都会收到ValueRequiredException
:
无法在字段' epcMemory'上满足@ org.simpleframework.xml.Attribute(empty =,name = epc_memory,required = true)第14行的de.sysvision.aviocheck.xml.generated.Object类的protected byte [] de.sysvision.aviocheck.xml.generated.Object.epcMemory
我使用此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>
这是我的Simple XML Object类:
@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);
}
}
如果我设置&#34;必需&#34;从epcMemory到false,那么这个工作!新的例外是:
无法在字段&#39; type&#39;上满足@ org.simpleframework.xml.Element(data = false,name =,required = true,type = void)第14行的de.sysvision.aviocheck.xml.generated.Object类的protected java.lang.String de.sysvision.aviocheck.xml.generated.Object.type
有人可以帮忙,并告诉我我做错了什么吗?
这是相应的XML架构:
<?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>
答案 0 :(得分:2)
答案很简单: 我不得不使用带有inline = true
的ElementList注释@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;
}
}