需要jar:dom4j-1.6.1.jar、jaxen-1.1.1.jar,下载地址:点击打开链接
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class Recursive {
public static void main(String[] args){
try{
File file = new File("C:\\Users\\lufei\\Documents\\Tencent Files\\609306779\\FileRecv\\ystadi2.xml");
FileReader reader = new FileReader(file);
StringBuffer sb = new StringBuffer();
char ch[] = new char[1024];
int d = reader.read(ch);
while(d != -1){
String str = new String(ch, 0, d);
sb.append(str);
d = reader.read(ch);
}
Document document = DocumentHelper.parseText(sb.toString());
Recursive shController = new Recursive();
Map<String, Object> map = shController.documentToMap(document);
Iterator<String> it = map.keySet().iterator();
while(it.hasNext()){
String key = it.next();
System.out.println(key + ":" + map.get(key));
}
}catch(Exception e){
e.printStackTrace();
}
}
private Map<String, Object> documentToMap(Document document){
Element element = document.getRootElement();
return recursive(element, new HashMap<String, Object>(), "");
}
/**
* 递归方法,穷尽element
* @param element
* @param map
* @param prevName
* @return
*/
private Map<String, Object> recursive(Element element, Map<String, Object> map, String prevName){
map.putAll(ElementToMap(element, prevName));
List<Element> list = element.elements();
if(list != null){
for(int i=0; i<list.size(); i++){
Element e = list.get(i);
recursive(e, map, prevName+"."+e.getName());
}
}
return map;
}
/**
*
* @param element
* @param prevName
* @return map key:<br>text:准备取元素值,attr:准备取属性值,最末尾数字表明在xml中出现的次序,从0开始
*/
private Map<String, Object> ElementToMap(Element element, String prevName){
if(!"".equals(prevName)) prevName = prevName + ".";
if(prevName.length()>0) prevName = prevName.substring(1,prevName.length());
Map<String, Object> attributeMap = new HashMap<String, Object>();
Map<String, Object> elementMap = new HashMap<String, Object>();
List<Element> list = element.elements();
if(list != null && list.size() > 0){
for(int i = 0; i < list.size(); i++){
Element e = list.get(i);
int elementCount = 0;
String elementKey = "text:"+prevName+e.getName();
Iterator<String> elementIt = elementMap.keySet().iterator();
while(elementIt.hasNext()){
String itKey = elementIt.next();
if((itKey.substring(0, itKey.lastIndexOf(".")).equals(elementKey))){
elementCount++;
}
}
elementKey = elementKey + "." + elementCount;
elementMap.put(elementKey, e.getTextTrim());
List<Attribute> atts = e.attributes();
for(int j=0; j<atts.size(); j++){
Iterator<String> attributrIt = attributeMap.keySet().iterator();
int attributeCount = 0;
String attributekey = "attr:"+prevName+e.getName()+"."+atts.get(j).getName();
while(attributrIt.hasNext()){
String itKey = attributrIt.next();
if((itKey.substring(0, itKey.lastIndexOf(".")).equals(attributekey))){
attributeCount++;
}
}
attributekey = attributekey +"."+attributeCount;
attributeMap.put(attributekey, atts.get(j).getValue());
}
}
}
elementMap.putAll(attributeMap);
return elementMap;
}
}