dom4j递归遍历

需要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;
	}
	
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值