Python实现灵活的xml文件解析
2012-10-09 15:26
555 查看
由于工作需要,需求是把任意xml文件的所有element(元素)解析成一个字典序列返回。
这里使用Python自带的xml解析库,非常好用,效率也不错。
import os
import xml.etree.ElementTree as ET
def parser_any_xml(filename):
if not filename or not os.path.isfile(filename) or not os.path.exists(filename):
print('xml file not exist or not a file path!')
return False
all_key_value = [ ]
key_value = {}
roots = ET.parse(filename).getroot()
items = roots.findall('*')
for e in items:
item = e.findall('*')
for i in item:
key_value[i.tag] = i.text
all_key_value.append(key_value)
key_value = {}
return all_key_value
这里使用Python自带的xml解析库,非常好用,效率也不错。
import os
import xml.etree.ElementTree as ET
def parser_any_xml(filename):
if not filename or not os.path.isfile(filename) or not os.path.exists(filename):
print('xml file not exist or not a file path!')
return False
all_key_value = [ ]
key_value = {}
roots = ET.parse(filename).getroot()
items = roots.findall('*')
for e in items:
item = e.findall('*')
for i in item:
key_value[i.tag] = i.text
all_key_value.append(key_value)
key_value = {}
return all_key_value
相关文章推荐
- 利用python将xml文件解析成html文件的实现方法
- python解析xml文件实例分析
- Python中使用ElementTree解析XML文件
- python解析XML文件
- python创建和解析xml文件
- java使用dom4j解析xml配置文件实现抽象工厂反射示例
- 解析xml文件,遍历输出xml文件中的所有节点, 最终模仿实现struts2框架
- Python实现解析Bit Torrent种子文件内容的方法
- python菜鸟升级路--自动化解析生成xml文件
- 用Python解析XML文件
- XML--- 简单的DOM解析XML文件,并实现增,删,查,改的操作
- Ajax实现xml文件数据插入数据库(一)--- 构建解析xml文件的js库
- python解析xml配置文件
- 用多线程实现解析XML文件
- Python解析XML文件
- Python语言解析xml文件
- Python 利用 XSD 文件,解析 XML 文件
- Python学习之解析xml文件
- Python(13) python解析xml文件
- 使用Python和xml.etree.ElementTree解析xml文件