XML文件解析

package com.atom.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.atom.basic.columntree.ColumnTree;
import com.atom.biz.base.calendar.bean.CalenderNode;
import com.atom.biz.base.setup.BaseSetupNode;

/**
 * <p>
 * 操作xml文件类
 * </p>
 * xml 文件格式为
 * 
 * <tables><table><col name="" value="" /><col name="" value="" /></table><table></table></tables>
 * 
 * @author KHT * @version $Id: XmlUtil.java,v 0.1 2009-2-22 下午10:49:02  */

public class XmlUtil {

	/**
	 * <p>
	 * 读取数据的xml文件
	 * </p>
	 * 
	 * @author Jack Zhou
	 * @version $Id: XmlUtil.java,	 */
	@SuppressWarnings("unchecked")
	public static List<BaseSetupNode> readBaseNodes(String path) {
		List<BaseSetupNode> list = new ArrayList<BaseSetupNode>();
		SAXReader reader = new SAXReader();
		Document doc = null;
		String leaf = "";
		try {
			// 读取文件
			File in = new File(path);
			doc = reader.read(in);
			Element root = doc.getRootElement();
			List<Element> eles = root.elements("nodes");
			List<Element> cols = null;
			for (Element ele : eles) {
				cols = ele.elements();
				for (Element element : cols) {
					BaseSetupNode node = new BaseSetupNode();
					node.setFz(element.attributeValue("fz"));
					node.setMc(element.attributeValue("mc"));
					node.setSm(element.attributeValue("sm"));
					leaf = element.attributeValue("leaf");
					node.setUiProvider("col");
					node.setId(element.attributeValue("id"));
					node.setParentId(element.attributeValue("parantid"));
					node.setHref(element.attributeValue("href"));
					if ("".equals(leaf) || "0".equals(leaf)) {
						node.setLeaf(false);
						node.setIconCls("task");
					} else {
						node.setLeaf(true);
						node.setIconCls("task-folder");
					}
					list.add(node);
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return list;
	}

	public static List<CalenderNode> readCalenderNodes(String path) {
		List<CalenderNode> list = new ArrayList<CalenderNode>();
		SAXReader reader = new SAXReader();
		Document doc = null;
		String leaf = "";
		try {
			// 读取文件
			File in = new File(path);
			doc = reader.read(in);
			Element root = doc.getRootElement();
			List<Element> eles = root.elements("nodes");
			List<Element> cols = null;
			String isv = "";
			for (Element ele : eles) {
				cols = ele.elements();
				for (Element element : cols) {
					CalenderNode node = new CalenderNode();
					node.setDateType(element.attributeValue("dateType"));
					node.setDateName(element.attributeValue("dateName"));
					node.setDate(element.attributeValue("date"));
					node.setMemo(element.attributeValue("memo"));
					isv = element.attributeValue("visible");
					if (!StringUtil.isEmpty(isv))
						node
								.setVisible("<img src='/public/images/Checkmark4.gif' />");
					else
						node.setVisible("-");
					leaf = element.attributeValue("leaf");
					node.setUiProvider("col");
					node.setId(element.attributeValue("id"));
					node.setParentId(element.attributeValue("parantid"));
					node.setHref(element.attributeValue("href"));
					if ("".equals(leaf) || "0".equals(leaf)) {
						node.setLeaf(false);
						node.setIconCls("task");
					} else {
						node.setLeaf(true);
						node.setIconCls("task-folder");
					}
					list.add(node);
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, String>> readMapTest() {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		String path = this.getClass().getResource("/").getPath()
				+ "/xml/test.xml";
		SAXReader reader = new SAXReader();
		Document doc = null;
		try {
			// 读取文件
			File in = new File(path);
			doc = reader.read(in);
			Element root = doc.getRootElement();
			List<Element> eles = root.elements("nodes");
			List<Element> cols = null;
			for (Element ele : eles) {
				cols = ele.elements();
				for (Element element : cols) {
					Map<String, String> map = new HashMap<String, String>();
					map.put("childrows", element.attributeValue("childrows"));
					map.put("hc0", element.attributeValue("hc0"));
					map.put("hc1", element.attributeValue("hc1"));
					map.put("hc2", element.attributeValue("hc2"));
					map.put("hc3", element.attributeValue("hc3"));
					map.put("hasnote", element.attributeValue("hasnote"));
					map.put("urid", element.attributeValue("urid"));
					map.put("entityid", element.attributeValue("entityid"));
					map.put("bankid", element.attributeValue("bankid"));
					list.add(map);
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return list;
	}

	public static void main(String[] args) {
		XmlUtil xml = new XmlUtil();
		String path = xml.getClass().getResource("/").getPath();
		List<BaseSetupNode> nodes = XmlUtil.readBaseNodes(path
				+ "/xml/base.xml");
		ColumnTree t = new ColumnTree();
		System.out.println(t.getJsonString(nodes));
		// System.out.println(nodes.size());
		// System.out.println(path);

	}

}
posted @ 2011-07-20 16:44  上善¤若水  阅读(139)  评论(0编辑  收藏  举报