java XML转List、Map(dom4j)

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

public class XmlToList {

	public static Map xmltoMap(Element nodeElement) {
		try {
			Map map = new HashMap();
			if (nodeElement.isTextOnly()) {
				map.put(nodeElement.getName(), nodeElement.getText());
			} else {
				List node = nodeElement.elements();
				Iterator it = node.iterator();
				while (it.hasNext()) {
					Element elm = (Element) it.next();
					if (!elm.isTextOnly()) {
						List lis = xmltoList(elm);
						map.put(elm.getName(), lis);
						lis = null;
					} else {
						map.put(elm.getName(), elm.getText());
					}
					elm = null;
				}
				node = null;
			}

			nodeElement = null;
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List xmltoList(Element nodesElement) {
		try {
			List list = new ArrayList();
			List nodes = nodesElement.elements();
			for (Iterator its = nodes.iterator(); its.hasNext();) {
				Element nodeElement = (Element) its.next();
				Map map = xmltoMap(nodeElement);
				list.add(map);
				map = null;
			}
			nodes = null;
			nodesElement = null;
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List xml2ListExecute(String xml) {
		try {
			List list = new ArrayList();
			Document document = DocumentHelper.parseText(xml);
			Element nodesElement = document.getRootElement();
			list = xmltoList(nodesElement);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Map xml2MapExecute(String xml) {
		try {
			Map map = new HashMap();
			Document document = DocumentHelper.parseText(xml);
			Element nodesElement = document.getRootElement();
			map = xmltoMap(nodesElement);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		String xmlStr = "<ROOT><ITEM><BBXH>000000021007</BBXH><ABS><AAA>111</AAA><BBB>222</BBB></ABS><QX_JGDM>130100000000000</QX_JGDM><BBMC>130100000统计报表</BBMC><JG_MC>130100000委</JG_MC><LRSJ>2013-12-04</LRSJ><SJSHRQ></SJSHRQ><SJSHR_DM></SJSHR_DM><ND>2013</ND><HZBZ>N</HZBZ></ITEM><ITEM><BBXH>20131204000000021008</BBXH><QX_JGDM>130100000000000</QX_JGDM><BBMC>130100000130100000报表</BBMC><JG_MC>130100000委</JG_MC><LRSJ>2013-12-04</LRSJ><SJSHRQ></SJSHRQ><SJSHR_DM></SJSHR_DM><ND>2013</ND><HZBZ>N</HZBZ></ITEM></ROOT>";
		String xmlStr1 = "<ROOT><ITEM><BBXH>345</BBXH></ITEM></ROOT>";
		String xmlStr2 = "<ROOT><ITEM><BBXH>20131204000000021007</BBXH><ABS><AAA>111</AAA><BBB>222</BBB></ABS><QX_JGDM>130100000000000</QX_JGDM><BBMC>130100000报表</BBMC><JG_MC>130100000130100000委</JG_MC><LRSJ>2013-12-04</LRSJ><SJSHRQ></SJSHRQ><SJSHR_DM></SJSHR_DM><ND>2013</ND><HZBZ>N</HZBZ></ITEM></ROOT>";
		/*
		 * List list = xml2ListExecute(xmlStr); for (Iterator its =
		 * list.iterator(); its.hasNext();) { Map map = (Map)its.next();
		 * if(map.get("ROOT")!=null){ System.out.println(map.get("ROOT")); }
		 * if(map.get("ITEM")!=null){ System.out.println(map.get("ITEM")); }
		 * if(map.get("ABS")!=null){
		 * System.out.println(((Map)((List)map.get("ABS")).get(0)).get("AAA"));
		 * }
		 * 
		 * 
		 * //
		 * System.out.println(((Map)((List)map.get("ABS")).get(0)).get("aaa"));
		 * }
		 */
		Map map = xml2MapExecute(xmlStr1);
		System.out.println(((Map) ((List) map.get("ITEM")).get(0)).get("BBXH"));
		// xmlStr2
		/*
		 * System.out.println(((Map)((List)map.get("ITEM")).get(0)).get("BBXH"));
		 * System.out.println(((Map)((List)map.get("ITEM")).get(1)).get("AAA"));
		 * System.out.println(((Map)((List)map.get("ITEM")).get(1)).get("BBB"));
		 */
	}
}

 

你可能感兴趣的:(java,xml)