Convert Java XML file to map collection

Source: Internet
Author: User
Tags gettext

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.