把一个xml节点信息递归的存到map中的方法

[size=x-large]jar包自己加!![/size]


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class XmlUtil {
public static Map doInfoXML(String infoXML) {
Document document;
Map map = new HashMap();
try {
document = DocumentHelper.parseText(infoXML);
Element root = document.getRootElement();
Iterator it = root.elements().iterator();
while (it.hasNext()) {
Element info = (Element) it.next();
map.put(info.getName(), info.getText());
Iterator itc = info.elements().iterator();
while (itc.hasNext()) {
Element infoc = (Element) itc.next();
map.put(infoc.getName(), infoc.getText());
}
}
} catch (DocumentException e1) {
e1.printStackTrace();
}
return map;
}

}


    推荐阅读