(de)serialize an object(Hashmap) from an XML of this form with XStream
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);
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.
精彩评论