xml查找
2016-02-05 15:47
323 查看
using System; using System.Collections.Generic; using System.Xml; using System.Text; public class SXML//节点类 { public bool hasFound = true;//是否找到 private XmlNode rootNode;//根节点 private XmlNode curNode;//当前节点 private string NodeName;//你需要查找的节点的名字 private int filtOper;//1"==" 2"!=" 3">" 4"<" 5">=" 6"<=" private string filtKey = "";//需要过滤的键 private string filtValue = "";//需要过滤的键的值 private int filtIntValue;//需要过滤的键的值的int类型 public SXML(XmlNode root, string nodename, string filter)//初始化时,找到条件满足的第一个节点 { rootNode = root; curNode = root; NodeName = nodename; filtOper = 0; if (filter != null)//如果过滤条件不是空 { int nstep = 0;//是否已经找到运算符 char[] c_arr = filter.ToCharArray(); for (int i = 0; i < c_arr.Length; i++) { char curkey = c_arr[i]; switch (curkey) { case '=': { nstep = 1; i++; filtOper = 1; //"==" } break; case '!': { nstep = 1; i++; filtOper = 2; //"!=" } break; case '>': { nstep = 1; if ('=' == c_arr[i + 1]) { i++; filtOper = 5; //">=" } else { filtOper = 3; //">"单个运算符i不需要加1 } } break; case '<': { nstep = 1; if ('=' == c_arr[i + 1]) { i++; filtOper = 6; //"<=" } else { filtOper = 4; //"<" } } break; default: { if (1 == nstep)//已找到运算符 { filtValue += curkey;//需要过滤的键的值 } else { filtKey += curkey;//需要过滤的键 } } break; } } if (filtOper >= 3 && filtOper <= 6)//如果是比大小的运算符 { filtIntValue = int.Parse(filtValue);//转换成int类型 } if (false == checkFilt())//此节点如果不满足条件 { nextOne();//下一个节点 } } } private bool checkFilt()//计算节点的属性是否满足条件 { if (curNode != null)//如果当前节点不为空 { bool bpass = false; switch (filtOper) { case 1: { if (true == getString(filtKey).Equals(filtValue)) { bpass = true; } } break; case 2: { if (false == getString(filtKey).Equals(filtValue)) { bpass = true; } } break; case 3: { int value = getInt(filtKey); if (value > filtIntValue) { bpass = true; } } break; case 4: { int value = getInt(filtKey); if (value < filtIntValue) { bpass = true; } } break; case 5: { int value = getInt(filtKey); if (value >= filtIntValue) { bpass = true; } } break; case 6: { int value = getInt(filtKey); if (value <= filtIntValue) { bpass = true; } } break; default: { bpass = false; } break; } hasFound = bpass;//满足条件 return bpass; } hasFound = false;//不满足 return false; } public bool nextOne()//移到下一个满足条件的节点 { while (curNode != null) { curNode = curNode.NextSibling;//移到下一个同级的节点 if (curNode != null && curNode.Name == NodeName)//如果节点不为空并且节点名字正确 { if (filtOper != 0)//有运算符 { if (checkFilt())//确认此节点是否满足条件 { return true; } } else { return true; } } } return false; } public SXML GetNode(string command, string filter) { if ( null == curNode ) return null; XmlNode xml_node = curNode; string[] cmds = command.Split('.');//用点切割命令 if (cmds.Length < 1) return null;//命令没有子节点 for (int i = 0; i < cmds.Length; i++)//按着深度一级级往下查找,如果找不到其中一个就返回空 { bool bfind_node = false;//是否已经找到节点 foreach (XmlNode child in xml_node.ChildNodes)//遍历子节点 { if (child.NodeType == XmlNodeType.Comment) continue; if (child.Name == cmds[i]) { bfind_node = true;//找到节点 xml_node = child;//如果碰到节点名字一样的,把这个节点作为父节点 break; } } if (false == bfind_node) { return null; } } string lastkey = cmds[cmds.Length - 1];//最后一个节点的名字.也就是我们需要查找的节点 return new SXML(xml_node, lastkey, filter);//返回sxml实例 } //////////////////////////////////////////////////////////////////////////////////////////////////// public string getString(string key) { if (curNode != null) { foreach (XmlAttribute attr in curNode.Attributes) { if (attr.Name == key) { return attr.Value; } } } return "null"; } public float getFloat(string key) { if (curNode != null) { foreach (XmlAttribute attr in curNode.Attributes) { if (attr.Name == key) { return float.Parse(attr.Value); } } } return -1f; } public int getInt(string key) { if (curNode != null) { foreach (XmlAttribute attr in curNode.Attributes) { if (attr.Name == key) { return int.Parse(attr.Value); } } } return -1; } public uint getUint(string key) { if (curNode != null) { foreach (XmlAttribute attr in curNode.Attributes) { if (attr.Name == key) { return uint.Parse(attr.Value); } } } return 0; } } public class XMLMgr//管理类,加载xml,获取节点 { private static XMLMgr _instance; public static XMLMgr instance { get { if(_instance==null) { _instance=new XMLMgr(); } return _instance; } } private XMLMgr() { } private Dictionary<string, XmlDocument> m_allConf = new Dictionary<string, XmlDocument>();//配置文件字典 public void AddXmlData(string key, ref string data)//添加配置文件 { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(data);//读路径 m_allConf[key] = xmlDoc;//放进字典 } public SXML GetSXML(string command, string filter=null) { string[] cmds = command.Split('.'); if (cmds.Length < 2) return null; if (m_allConf.ContainsKey(cmds[0])) { XmlDocument xml_doc = m_allConf[cmds[0]]; XmlNode xmlNode = xml_doc.DocumentElement;//获取根节点 for (int i = 1; i < cmds.Length; i++)//一级一级遍历 { bool bfind_node = false; foreach (XmlNode child in xmlNode.ChildNodes) { if (child.NodeType == XmlNodeType.Comment) continue; if (child.Name == cmds[i]) { bfind_node = true; xmlNode = child; break; } } if (false == bfind_node) { return null; } } string lastkey = cmds[cmds.Length - 1];//最后一个节点的名字 return new SXML(xmlNode, lastkey, filter);//初始化sxml } return null; } }
相关文章推荐
- IOS调试lldb命令常用po
- 你的计划为什么执行不下去?
- cmake制作deb包
- Ceph命令之ceph
- BZOJ-1901 Zju2112 Dynamic Rankings 函数式线段树 套 树状数组+离线处理
- HD 2177(威佐夫博弈 入门)
- ucore中Makefile 内核文件组织全解析,学习软件的组织方式
- 简单DOM解析之dom4j(二)
- ODS
- 马云最新演讲:我是瞎子骑瞎老虎背上,但活了下来
- 老项目换unicorn的大致步骤
- OJ删除重复字符串
- Subsequence
- 老项目换unicorn的大致步骤
- 主从数据库架构
- eclipse xml 注释快捷键
- App架构设计经验谈:服务端接口的设计
- Unix Philosophy
- Ceph Calamari组件之Diamond
- Broken Keyboard 2010.3.6