[Simple-support] Transitioning from ElementList to ElementMap
Brought to you by:
niallg
|
From: Kiran R. <tec...@gm...> - 2013-01-16 14:52:54
|
I have an XML format that looks like this:
<devices>
<device id="10">
<name>Nexus 7</name>
<os>Android</os>
</device>
<device id="20">
<name>Samsung Galaxy S3</name>
<os>Android</os>
</device>
<device id="30">
<name>Apple iPad3</name>
<os>iOS</os>
</device>
</devices>
Currently, I am treating this as an ElementList as follows:
@Root(name="devices")
class Devices{
@ElementList(inline="true")
List<Device> devices;
// ... Constructors, getters, setters
}
@Root(name="device")
class Device{
@Attribute(name="id")
long id;
@Element(name="name")
String name;
@Element(name="os")
String os;
//... Constructors, getters, setters
}
So far, so good. I have been parsing this as an ArrayList. However, *I
almost always want to access a device by ID*, not by its position. So, I
thought, I'd instead parse this as a Map, not a List. I can map a device ID
to the device object itself.
I tried to use ElementMap annotation, but I can't seem to get it right.
Here's what I've tried:
@Root(name="devices")
public class DeviceMap {
@ElementMap(entry="device", key="id", attribute = true, inline = true)
private Map<Long, Device> map;
public Map<Long, Device> getMap(){
return this.map;
}
}
@Root(name = "device")
public class Device {
@Element(name="os")
String os;
@Element(name="name")
String name;
//... Constructors, getters, setters}
}
However, this fails with a ValueRequiredException:
org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy
@org.simpleframework.xml.Element(name=OS, data=false, required=true,
type=void) on field 'os' java.lang.String
*Note that the value of the Map in my case turns out to be neither a
primitive, nor an object - it is a bunch of what would have been inline
elements.
*
Any pointers on how to proceed with this?
--
Regards,
=======
Kiran Rao
http://curioustechizen.blogspot.com/
http://stackoverflow.com/users/570930/curioustechizen
|