python读取xml文件
2014-04-23 18:39
316 查看
xml = '''<?xml version="1.0" encoding="utf-8"?> <Conf> <DBconf> <DB> <Desc>abc </Desc> <ConnStr>DB_192.168.3.1 </ConnStr> <UserName>system </UserName> <PassWd>aa </PassWd> </DB> <DB> <Desc>bb </Desc> <ConnStr>ORA9i_192.168.3.1 </ConnStr> <UserName>system </UserName> <PassWd>system </PassWd> </DB> <DB> <Desc>ddd </Desc> <ConnStr>ORA9i_192.168.3.1 </ConnStr> <UserName>system </UserName> <PassWd>system </PassWd> </DB> </DBconf> <Otherconf> <a>aaa </a> <c>aaa </c> </Otherconf> </Conf> ''' from xml.dom import minidom def get_childern(node): return filter(lambda n: isinstance(n, minidom.Element), node.childNodes) doc = minidom.parseString(xml) root = doc.childNodes[0] DBconf, Otherconf = get_childern(root) for DB in get_childern(DBconf): for n in get_childern(DB): print n.tagName, n.firstChild.data print for n in get_childern(Otherconf): print n.tagName, n.firstChild.data
示例结果:
Desc abc
ConnStr DB_192.168.3.1
UserName system
PassWd aa
Desc bb
ConnStr ORA9i_192.168.3.1
UserName system
PassWd system
Desc ddd
ConnStr ORA9i_192.168.3.1
UserName system
PassWd system
a aaa
c aaa
相关文章推荐
- python读取xml文件
- Python读取XML配置文件小例子
- python读取xml文件
- 利用python读取xml布局文件下面使用中文或者引用资源文件里面的中文,并且显示行号
- python读取xml文件
- python 读取 .xml 文件
- python读取xml文件
- python读取xml文件
- Python解析大XML文件及读取XML不全的问题
- python读取xml文件
- python读取xml文件
- Python读取与更改xml文件
- 【Python】Pandas读取tsv文件
- Python读取Excel文件遇到的编码问题(pycharm)
- C# 创建,读取,写入 XML文件
- python中文件的按行读取
- python学习笔记(四)-文件的读取、写入和复制、剪切
- SSH读取applicationContext.xml配置文件测试hibernate方法
- Python sftp到远程服务器读取日志文件
- Hadoop如何读取复杂格式的文件,例如XML、HTML、图像等,附源码