Re: [Simple-support] Transitioning from ElementList to ElementMap
Brought to you by:
niallg
|
From: Kiran R. <tec...@gm...> - 2013-01-17 05:43:17
|
Yes! That did it.
Using the Dictionary data structure provided by simple framework did the
trick. This is exactly what I was looking for. Thanks!
On Thu, Jan 17, 2013 at 9:37 AM, Niall Gallagher - Yieldbroker <
Nia...@yi...> wrote:
> Try****
>
> @Root****
>
> class Devices{****
>
> @ElementList(entry="device", inline=true)****
>
> org.simpleframework.xml.utl.Dictionary<Device> list;****
>
> }****
>
> @Root****
>
> class Device {****
>
> @Attribute****
>
> int id;****
>
> @Element****
>
> String name;****
>
> @Element****
>
> String os;****
>
> }****
>
> ** **
>
> ** **
>
> ** **
>
> *From:* Kiran Rao [mailto:tec...@gm...]
> *Sent:* Thursday, 17 January 2013 2:48 PM
> *To:* Niall Gallagher - Yieldbroker
> *Subject:* Re: [Simple-support] Transitioning from ElementList to
> ElementMap****
>
> ** **
>
> If I add required = false on all elements of Device, I get this:
> org.simpleframework.xml.core.ElementException: Element 'os' does not have
> a match in class org.github.curioustechizen.simplexml.map.DeviceMap at line
> 1****
>
> So, I also added strict = false on the @Root annotation for DeviceMapclass. Now, the parsing is successful, but as you probably suspect, the
> fields of the Device object are all null.****
>
> From what I understand, my use case is a special case since I am
> attempting to map a Device ID to the entire Device object itself. The
> mapped value is neither a primitive, nor a full-fledged element - it is a
> collection of elements at the same level.
>
> Is this even supported?****
>
> ** **
>
> On Thu, Jan 17, 2013 at 5:45 AM, Niall Gallagher - Yieldbroker <
> Nia...@yi...> wrote:****
>
> On the @Element use required=false****
>
> ****
>
> *From:* Kiran Rao [mailto:tec...@gm...]
> *Sent:* Thursday, 17 January 2013 1:53 AM
> *To:* simple-support
> *Subject:* [Simple-support] Transitioning from ElementList to ElementMap**
> **
>
> ****
>
> 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****
>
>
>
>
> --
> Regards,
> =======
> Kiran Rao
> http://curioustechizen.blogspot.com/
> http://stackoverflow.com/users/570930/curioustechizen****
>
--
Regards,
=======
Kiran Rao
http://curioustechizen.blogspot.com/
http://stackoverflow.com/users/570930/curioustechizen
|