gpt4 book ai didi

java - 简单的 XML : ValueRequiredException

转载 作者:行者123 更新时间:2023-11-30 09:06:51 31 4
gpt4 key购买 nike

我卡住了:我在我的代码中没有看到任何错误,但每次反序列化我的 XML 时都会收到 ValueRequiredException:

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

我正在使用这个 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>

这是我的简单 XML 对象类:

    @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);
}
}

如果我将 epcMemory 中的“required”设置为 false,那么就可以了!新的异常是:

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

任何人都可以帮助并告诉我我做错了什么吗?

这是相应的 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>

最佳答案

答案很简单:我必须使用带有 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;
}

}

关于java - 简单的 XML : ValueRequiredException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24266510/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com