tags:

views:

48

answers:

1

XML:

JAVA Hashmap: map = {key1=text1,key2=text2}

this doesn't work. why?

String xml = "<nodes><node id=\"key1\"><![CDATA[text1]]></node><node id="\key2\"><![CDATA[text2]]></node></nodes>";

XStream xs = new XStream();
xs.alias("nodes", Map.class);
xs.alias("node", String.class);
xs.useAttributeFor("id",String.class);
Map<String,String> map= (Map<String,String>) xs.fromXML(xml);
System.out.println(map);
+1  A: 

If you can define your XML structure you should check the Map Converter and adjust your XML.

If not, you should write your own custom converter. You can see this thread to check an implementation similar to your needs.

Lombo