5

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.

1 Answer 1

6

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?

Sign up to request clarification or add additional context in comments.

8 Comments

Yes, I did just that in fact, but setting attribute=true sets the key as the attribute, and not the value.
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).
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.
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 :).
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)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.