手机
当前位置:查字典教程网 >脚本专栏 >python >Python获取任意xml节点值的方法
Python获取任意xml节点值的方法
摘要:本文实例讲述了Python获取任意xml节点值的方法。分享给大家供大家参考。具体实现方法如下:#-*-coding:utf-8-*-impo...

本文实例讲述了Python获取任意xml节点值的方法。分享给大家供大家参考。具体实现方法如下:

# -*- coding: utf-8 -*- import xml.dom.minidom ELEMENT_NODE = xml.dom.Node.ELEMENT_NODE class SimpleXmlGetter(object): def __init__(self, data): if type(data) == str: self.root = xml.dom.minidom.parse(data) else: self.root = data def __getattr__(self, name): #support . operation if name == 'data': return self.root.firstChild.data for c in self.root.childNodes: if c.nodeType == ELEMENT_NODE and c.tagName == name: return SimpleXmlGetter(c) def __getitem__(self, index): #support [] operation eNodes = [ e for e in self.root.parentNode.childNodes if e.nodeType == ELEMENT_NODE and e.tagName == self.root.tagName] return SimpleXmlGetter(eNodes[index]) def __call__(self, *args, **kwargs): #support () openration, for query conditions for e in self.root.parentNode.childNodes: if e.nodeType == ELEMENT_NODE: for key in kwargs.keys(): if e.getAttribute(key) != kwargs[key]: break else: return SimpleXmlGetter(e) if __name__ == "__main__": x = SimpleXmlGetter("sysd.xml") print x.sysd.sysagent.param[2].data print x.sysd.sysagent.param(name="querytimeout", type="second").data

希望本文所述对大家的Python程序设计有所帮助。

【Python获取任意xml节点值的方法】相关文章:

tornado捕获和处理404错误的方法

Python调用C/C++动态链接库的方法详解

python连接sql server乱码的解决方法

Python下singleton模式的实现方法

Python学习笔记_数据排序方法

python在linux中输出带颜色的文字的方法

python中的yield使用方法

Python 条件判断的缩写方法

python去掉字符串中重复字符的方法

Python中使用动态变量名的方法

精品推荐
分类导航