为什么赢得SimpleXML反序列化属性?

时间:2016-12-27 17:11:30

标签: android soap simple-framework

我正在使用UPnP设备,它公开我想要访问的服务。我正在使用SimpleXML来编组数据。到目前为止一直很好,除了现在我再次陷入困境。

鉴于下面的XML

<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <item id="123456" parentID="1" restricted="1">
        <res protocolInfo="http-get:*:video/mpeg:*">http://stream_resource/media/index.m3u8</res>
        <upnp:callSign>My Call Sign here</upnp:callSign>
        <upnp:class>object.item.videoItem.videoBroadcast</upnp:class>
        <dc:title>My Title Here</dc:title>
    </item>
</DIDL-Lite>

我有以下 POJOs

根:

@Root(name = "DIDL-Lite")
@NamespaceList({
        @Namespace(reference = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"),
        @Namespace(reference = "urn:schemas-upnp-org:metadata-1-0/upnp/", prefix = "upnp"),
        @Namespace(reference = "http://purl.org/dc/elements/1.1/", prefix = "dc")
})
public class ResultObject {

  @ElementList(name = "item")
  private List<ObjectItem> listItems;
}

ObjectItem:

@Root(name = "item")
public class ObjectItem {

  @Attribute(name = "id")
  private String id;

  @Attribute(name = "parentID")
  private String parentID;

  @Attribute(name = "restricted")
  private String restricted;

  @Element(name = "res")//something appears to be wrong here ! this element is not actually parsed ?
  private ResourceInfo resInfo;

  @Element(name = "callSign")
  private String callSign;

  @Element(name = "class")
  private String upnpClass;

  @Element(name = "title")
  private String dcTitle;
}

ResourceInfo中:

@Root(name = "res")
public class ResourceInfo {

  @Attribute(name = "protocolInfo")
  private String protocolInfo;
}

这是我得到的解析错误:W / System.err:org.simpleframework.xml.core.AttributeException:Attribute&#39; protocolInfo&#39;在第1行的xx.yyy.ObjectItem类中没有匹配项。

经过一番挖掘后,我尝试将该值反序列化为ElementMap,如下所示:

ObjectItem:

@Root(name = "item")
public class ObjectItem {

  @Attribute(name = "id")
  private String id;

  @Attribute(name = "parentID")
  private String parentID;

  @Attribute(name = "restricted")
  private String restricted;

  @ElementMap(entry = "res", key = "protocolInfo", attribute = true, inline = true)
  //so what is actually going on here?
  private Map<String, String> elementMap;

  @Element(name = "callSign")
  private String callSign;

  @Element(name = "class")
  private String upnpClass;

  @Element(name = "title")
  private String dcTitle;

仍然解析错误。

任何提示?

1 个答案:

答案 0 :(得分:1)

问题不在ObjectItem中,而是ObjectItems存储在ResultObject中的方式。

@ElementList(name = "item", inline = true)上使用List<ObjectItem> listItems;代替@ElementList(name = "item")

或者在这种情况下不需要@ElementList(inline = true)名称。

见差异:

@ElementList

@ElementList(inline = true)