为什么 SimpleXML 不反序列化属性?
Why won't the SimpleXML deserialize Attribute?
我正在使用 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>
我有以下 POJO:
根目录:
@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;
}
对象项目:
@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;
}
资源信息:
@Root(name = "res")
public class ResourceInfo {
@Attribute(name = "protocolInfo")
private String protocolInfo;
}
这是我得到的解析错误:W/System.错误:org.simpleframework.xml.core.AttributeException:属性'protocolInfo'在class xx.yyy.ObjectItem 在第 1 行。
经过一些挖掘,我尝试将该值反序列化为 ElementMap
,如下所示:
对象项目:
@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;
仍然收到 解析错误。
有什么提示吗?
问题不在 ObjectItem 中,而是 ObjectItem 如何存储在 ResultObject 中。
在 List<ObjectItem> listItems;
上使用 @ElementList(name = "item", inline = true)
而不是 @ElementList(name = "item")
或者在这种情况下只需要 @ElementList(inline = true)
名称。
查看差异:
我正在使用 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>
我有以下 POJO:
根目录:
@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;
}
对象项目:
@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;
}
资源信息:
@Root(name = "res")
public class ResourceInfo {
@Attribute(name = "protocolInfo")
private String protocolInfo;
}
这是我得到的解析错误:W/System.错误:org.simpleframework.xml.core.AttributeException:属性'protocolInfo'在class xx.yyy.ObjectItem 在第 1 行。
经过一些挖掘,我尝试将该值反序列化为 ElementMap
,如下所示:
对象项目:
@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;
仍然收到 解析错误。
有什么提示吗?
问题不在 ObjectItem 中,而是 ObjectItem 如何存储在 ResultObject 中。
在 List<ObjectItem> listItems;
上使用 @ElementList(name = "item", inline = true)
而不是 @ElementList(name = "item")
或者在这种情况下只需要 @ElementList(inline = true)
名称。
查看差异: