Why won't the SimpleXML deserialize Attribute?

245 views Asked by At

I am working with a UPnP device, that exposes services I want to access. I am using SimpleXML for marshalling data. So far so good, except that now I am stuck, again.

Given the XML below:

<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>

I have the following POJOs:

Root:

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

This is the parse error that I get : W/System.err: org.simpleframework.xml.core.AttributeException: Attribute 'protocolInfo' does not have a match in class xx.yyy.ObjectItem at line 1.

After some digging, I tried to deserialize that value into an ElementMap like so:

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;

Still getting the parse error.

Any hints?

1

There are 1 answers

1
Anton Pogonets On BEST ANSWER

Problem is not in ObjectItem it is how ObjectItems stored in ResultObject.

Use @ElementList(name = "item", inline = true) on List<ObjectItem> listItems; instead of @ElementList(name = "item")

Or just @ElementList(inline = true) name is not required in this case.

See differences:

@ElementList

@ElementList(inline = true)