public class Xmltomap {
public static map<string, object> Dom2map (Document doc) {
map<string, object> map = new hashmap<string, object> ();
if (doc = = null)
return map;
Element root = Doc.getrootelement ();
for (Iterator Iterator = Root.elementiterator (); Iterator.hasnext ();) {
Element e = (Element) Iterator.next ();
System.out.println (E.getname ());
List List = E.elements ();
if (list.size () > 0) {
Map.put (E.getname (), Dom2map (e));
}else
Map.put (E.getname (), E.gettext ());
}
return map;
}
public static Map Dom2map (Element e) {
Map map = new HashMap ();
List List = E.elements ();
if (list.size () > 0) {
for (int i = 0;i < List.size (); i++) {
Element iter = (element) list.get (i);
List maplist = new ArrayList ();
if (iter.elements (). Size () > 0) {
Map m = Dom2map (ITER);
if (Map.get (Iter.getname ()) = null) {
Object obj = Map.get (Iter.getname ());
if (!obj.getclass (). GetName (). Equals ("Java.util.ArrayList")) {
Maplist = new ArrayList ();
Maplist.add (obj);
Maplist.add (m);
}
if (Obj.getclass (). GetName (). Equals ("Java.util.ArrayList")) {
Maplist = (List) obj;
Maplist.add (m);
}
Map.put (Iter.getname (), maplist);
}else
Map.put (Iter.getname (), m);
}
else{
if (Map.get (Iter.getname ()) = null) {
Object obj = Map.get (Iter.getname ());
if (!obj.getclass (). GetName (). Equals ("Java.util.ArrayList")) {
Maplist = new ArrayList ();
Maplist.add (obj);
Maplist.add (Iter.gettext ());
}
if (Obj.getclass (). GetName (). Equals ("Java.util.ArrayList")) {
Maplist = (List) obj;
Maplist.add (Iter.gettext ());
}
Map.put (Iter.getname (), maplist);
}else
Map.put (Iter.getname (), Iter.gettext ());
}
}
}else
Map.put (E.getname (), E.gettext ());
return map;
}
}
Convert Java XML file to map collection