python 模块- 对XML的解析 ElementTree(元素树)
来源:互联网 发布:大数据阅读材料文答案 编辑:程序博客网 时间:2024/06/06 21:39
参考地址:http://blog.csdn.net/yueguanghaidao/article/details/7265246
python的官方文档:https://docs.python.org/2/library/xml.etree.elementtree.html
前述,库文件说明:
Source code: Lib/xml/etree/ElementTree.py
导入的库文件为:from xml.etree import ElementTree
为了使用方便可以像命名别名方式:
from xml.etree import ElementTree as ET
理解:
element是个容器对象,有些属性:
1) 一个tag
2) 一些attrib,或者为空 {}
3) 一个text
4) 一些子element
它是介于list和dict的中间体容器。
find, findall 结果就是一个list( find和 findall的区别, find是查找第一tag对应的element下所有元素, findall是查找对应tag的所有elements)
root.attrib 结果就是一个dict
具体使用
下面是一个转载的例子:
test.xml如下:
<?xml version="1.0"?><data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank>68</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country></data>
1.加载xml文件
加载XML文件共有2种方法,一是加载指定字符串,二是加载指定文件
a) parse 加载xml文件
b) fromstring 加载指定字符串
2.获取element的方法
a) 通过getiterator
b) 过 getchildren
c) find方法
d) findall方法
通过例子学习下:
from xml.etree import ElementTree as ET#coding=gbktree = ET.parse("test.xml")root = tree.getroot()print root.tag, root.attrib, root.textfor child in root:print child.tag, child.attrib, root.text#getiterator方法, 得到对应tag的所有elemnt的countrys = root.getiterator("country")for country in countrys:print "===", country.tag, country.attrib#得到root下的所有子元素childs = root.getchildren()for child in childs:print "+++", child.tag, child.attrib#find方法,查找对应tag的第一个elment下的elements,支持pathfchilds = root.find("country")for fchild in fchilds:print "---", fchild.tag, fchild.attrib#findall方法,查找所有的对应tag的所有element, 支持pathfachilds = root.findall("country")for fachild in fachilds:print "---s", fachild.tag, fachild.attribfapchilds = root.findall("country/neighbor")for fapchild in fapchilds:print "---sp", fapchild.tag, fapchild.attrib
注意问题:
中文编码问题,ElementTree只支持 utf8编码
所以当时gbk编码的时候,需要转码。
字符串转码:str.decode('gbk').encode('utf8'), 并且xml的头文件encoding为gbk的需要修改,如:
<?xml version='1.0' encoding='GBK'?>
则需要str=str.replace('GBK', 'utf-8')
- python 模块- 对XML的解析 ElementTree(元素树)
- python解析XML:ElementTree(元素树)
- python使用elementtree模块解析xml
- python对XML的解析 SAX DOM ElementTree
- python的XML处理模块ElementTree
- python xml解析ElementTree
- python解析xml ElementTree
- Python中使用ElementTree对XML文件进行解析
- Python使用ElementTree解析XML
- python 使用ElementTree解析XML
- Python 使用ElementTree解析XML
- Python中用ElementTree解析XML
- python xml.etree.ElementTree解析
- Python中用ElementTree解析XML
- 使用Python库ElementTree解析Hadoop的xml配置文件
- python xml.etree ElementTree解析 编辑 xml
- python XML解析--xml.etree.ElementTree
- python-xml.etree.ElementTree解析xml文件
- 同步synchronized方法和代码块
- Request对象的主要方法
- struts2中的值栈
- Hibernate常见配置
- 最多几个点在一条直线上(poj1118 poj2606 poj2780)
- python 模块- 对XML的解析 ElementTree(元素树)
- hdu 3072 Intelligence System(强连通分量)
- Djago入门手记
- js在datatables中运用checkbox
- Lucene 评分机制
- ping 127.0.0.1 一般故障
- JSP内置对象及作用
- Mysql InnoDB 引擎独立表空间与共享表空间详解
- 著名的菲尔人格测试,看看你适合做什么类型的工作