tags:

views:

57

answers:

1

How would you go about serialising a Map using simple XML so that it looks something like:

<elem foo="value">key</elem>

Instead of the normal

<elem foo="key">value</elem>

(The map is one to many, and since this will be edited by humans, I wanted it to be clearer.)

[EDIT]: Doesn't Fix.

+2  A: 

Have you tried something like:

@ElementMap(entry="property", value="value", attribute=true, inline=true)
private Map<String, String> map;

or some combination, i.e. to use the other attributes of the @ElementMap annotation too?

A. Ionescu
Yes, I did just that in fact, but setting `attribute=true` sets the key as the attribute, and not the value.
brice
thanks for stopping by anyway ;-)
brice
so using value="value" instead of key="key" will not put them in reverse order? If so than that's a bug I think. To make it still work, you need to define your own template (see the docs) or as an alternative to make your own @ElementMap annotation (e.g. you can call it @ElementMapReversed that reversed that (until the team fixes that bug).
A. Ionescu
I don't think that the `key` and `value` parameters are used like this. These parameters define what the xml element is called when `inline=false`. For example: `@ElementMap(entry="spam",value="foo", key="bar")` would yield `<spam><bar>key</bar><foo>value</foo></spam>` for each K,V pairs.
brice
Yes, when inline=false, and when inline=true than they should represent the name of the attribute for the mentioned element. Since only value="value" would used, than it would be just logical to inline only that part, so the key would remain as text. If both value="value" and key="key" would be present, than the result should look like: <entry value="xxx" key="yyy"/>. I think just that would be "simple", as the developers market their framework :).
A. Ionescu
Nope, it doesn't work like that. `inline` leaves the value as text. and the attribute only refers to the key, not the value. Try it for yourself. You still get `<foo> <property key="key2">value2</property> <property key="key">value</property> </foo>`(the above is actual output with `inline=true`)
brice
Yes, indeed :( . Sorry for that: I would have 100% expected to work like I described above (I've used SimpleXML in a few projects and I also recommended it everywhere, but this behavior here is really counter-intuitive).
A. Ionescu
Doesn't matter, in the end I just ripped the key,value pair into a separate class and got it working the way I wanted (actually more flexibly). I never needed the O(1) access by keys.
brice
Hey, since you're the only person who replied, I'm giving you the answer, even if it doesn't fix.
brice