tags:

views:

72

answers:

4

If I have Map setup like:

map.put("foo", "123");
map.put("bar", "456");
map.put("baz", "789");

then I want to do something like:

  for (String key : map.keySet().toArray(new String[0])) {
    // marshall out to .xml a tag with the name key and the
    // value map.get(key)
  }

So what it will marshal out is something like:

<map>
  <foo>123</foo>
  <bar>456</bar>
  <baz>789</baz>
</map>

Can I do this with some fancy JAXB annotations or is there something else that lends it self to dynamic element names?

TIA

+2  A: 

I don't think that there are annotations supporting this. Take a look at:

Mapping your favorite class

tkr
A: 

Do you have to use JAXB? I've never used that, but I have had a lot of success with XStream. It's very flexible so you to get the XML you want.

mdma
A: 

I decided to go with straight SAX output, much easier since I really didnt want to do O/X mapping anyway. I was really try to avoid writing code :)

A: 

like,

  for (String key : map.keySet()) {
      System.out.printf("<%s>%s</%s>", key, map.get(key), key);
  }

??

irreputable
You might want to escape your outputs as per the XML specification.
McDowell
irreputable