I'm searching a lightweight API (preferable single class) to convert a
Map<String,String> map = new HashMap<String,String();
to xml and, vice versa, convert the XML back to a Map.
example:
Map<String,String> map = new HashMap<String,String();
map.put("name","chris");
map.put("island","faranga");
MagicAPI.toXML(map,"root");
result:
<root>
<name>chris</chris>
<island>faranga</island>
</root>
and back:
Map<String,String> map = MagicAPI.fromXML("...");
I dont want to use JAXB or JSON conversion API. It doesnt have to take care of nested maps or attributes or anything else, just that simple case. Any suggestions?
Edit: I created a working copy & paste sample. Thanks to fvu and Michal Bernhard.
Download latest XStream framework, 'core only' is enough.
Map<String,Object> map = new HashMap<String,Object>();
map.put("name","chris");
map.put("island","faranga");
// convert to XML
XStream xStream = new XStream(new DomDriver());
xStream.alias("map", java.util.Map.class);
String xml = xStream.toXML(map);
// from XML, convert back to map
Map<String,Object> map2 = (Map<String,Object>) xStream.fromXML(xml);
No converters or anything else is required. Just the xstream-x.y.z.jar is enough.