封送映射到。xml

时间:2021-11-15 19:35:29

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?

我是否可以使用一些花哨的JAXB注释来实现这一点呢?还是有其他的东西将它自身借给动态元素名呢?

TIA

蒂雅

3 个解决方案

#1


0  

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.

需要使用JAXB吗?我从未使用过它,但是我在XStream上取得了很大的成功。它非常灵活,所以您可以获得所需的XML。

#2


0  

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 :)

我决定使用直接的SAX输出,这要容易得多,因为我实在不想做O/X映射。我真的尽量避免写代码:)

#3


0  

like,

就像,

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

??

? ?

#1


0  

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.

需要使用JAXB吗?我从未使用过它,但是我在XStream上取得了很大的成功。它非常灵活,所以您可以获得所需的XML。

#2


0  

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 :)

我决定使用直接的SAX输出,这要容易得多,因为我实在不想做O/X映射。我真的尽量避免写代码:)

#3


0  

like,

就像,

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

??

? ?