mapping key value xml pair to java map using xstream -


i trying convert xml file follows java map. xml

<person>    <id>123</id>    <demographics>    <lastname>abc</lastname>    <firstname>xyz</firstname>    </demographics>    <married>yes</married> </person> 

the xstream code follows:

    final xstream xstream = new xstream();     xstream.alias("person", map.class);     xstream.alias("demographics", map.class);     xstream.registerconverter(new mapentryconverter());      final map<string, object> map2 = (map<string, object>) xstream.fromxml(xml);//where xml above defined string. 

the custom mapentryconverter is:

public class mapentryconverter implements converter {     public boolean canconvert(final class clazz)     {         return abstractmap.class.isassignablefrom(clazz);     }      public void marshal(final object value, final hierarchicalstreamwriter writer, final marshallingcontext context)     {         final abstractmap<string, string> map = (abstractmap<string, string>) value;         (final entry<string, string> entry : map.entryset())         {             writer.startnode(entry.getkey().tostring());             writer.setvalue(entry.getvalue().tostring());             writer.endnode();         }     }      public object unmarshal(final hierarchicalstreamreader reader, final unmarshallingcontext context)     {         final map<string, string> map = new hashmap<string, string>();         while (reader.hasmorechildren())         {             reader.movedown();             map.put(reader.getnodename(), reader.getvalue());             reader.moveup();         }         return map;     } } 

so map consists of id , married key-value pairs. not converting demographics parent corresponding name value pairs.

i have tag names keys , values map values.

make use of convertanother method on marshallingcontext handle hierarchical nature of data, e.g.:

public class mapentryconverter implements converter {      public boolean canconvert(final class clazz) {         return map.class.isassignablefrom(clazz);     }      public void marshal(final object value, final hierarchicalstreamwriter writer, final marshallingcontext context) {         map<string, object> map = (map<string, object>) value;         (map.entry<string, object> entry : map.entryset()) {             if (entry.getvalue() instanceof map) {                 writer.startnode(entry.getkey());                 context.convertanother(entry.getvalue());                 writer.endnode();             } else {                 writer.startnode(entry.getkey());                 writer.setvalue(entry.getvalue().tostring());                 writer.endnode();             }         }     }      public object unmarshal(final hierarchicalstreamreader reader, final unmarshallingcontext context) {         map<string, object> map = new hashmap<string, object>();         while (reader.hasmorechildren()) {             reader.movedown();             if (reader.hasmorechildren()) {                 map<string, object> childmap = (map<string, object>) context.convertanother(new hashmap<string, object>(), map.class);                 map.put(reader.getnodename(), childmap);             } else {                 map.put(reader.getnodename(), reader.getvalue());             }              reader.moveup();         }         return map;     } } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -