zoukankan      html  css  js  c++  java
  • 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);
    
    	}
    
    }
    
  • 相关阅读:
    Graphics竖排打印字体
    VC GetDlgItem
    C# FontStyle
    VC弹出"选择文件"和"选择文件夹"对话框(转)
    VC获取应用程序路径
    SqlServer延时函数
    volatile修饰变量
    外部命令和内部命令
    curl命令简单使用
    close_socket断开连接的方式
  • 原文地址:https://www.cnblogs.com/qq1988627/p/6606928.html
Copyright © 2011-2022 走看看