ElementException的SimpleXML问题 - 没有匹配

时间:2018-03-13 18:51:14

标签: android xml simple-framework

输入XML:

<DeviceInfo dpId="..." rdsId="..." rdsVer="..." dc="..." mi="..." mc="...">
<additional_info>
<Param name="..." value="..." />
<Param name="..." value="..." />
</additional_info>
</DeviceInfo>

实体DeviceInfo

@Root(name="DeviceInfo")
public class DeviceInfo {

        // Mandatory
        @Attribute(name="dpId")
        public String DpId;
        @Attribute(name="rdsId")
        public String RdsId;
        @Attribute(name="rdsVer")
        public String RdsVer;
        @Attribute(name="dc")
        public String Dc;
        @Attribute(name="mi")
        public String Mi;
        @Attribute(name="mc")
        public String Mc;

        @Element (name="additional_info")
        public AdditionalInfo AdditionalInfo;

        // Constructor
        // ...
        // Getter & Setter
        // ...

        public static class AdditionalInfo
        {
            @ElementList(name="Param",entry="Parameters", inline = true)
            public List<DeviceInfoParam> Parameters;

            AdditionalInfo()
            {
            }
        }

        public static class DeviceInfoParam
        {
            @Attribute(name="name")
            public String Name;
            @Attribute(name="value")
            public String Value;

            DeviceInfoParam()
            {
            }
        }

}

问题: W / System.err:org.simpleframework.xml.core.ElementException:元素'Param'在第3行的类x.x.entities.DeviceInfo $ AdditionalInfo中没有匹配

问题: 实体中缺少什么(我无法更改XML!)?我尝试了各种各样的变体,包括元素而不是元素列表,添加条目,内联等。

1 个答案:

答案 0 :(得分:0)

在注释中,您需要添加required = false。如果没有匹配项,它将被跳过。例如:

@ElementList(name="Param",entry="Parameters", inline = true, required = false)
        public List<DeviceInfoParam> Parameters;