Python学习-ElementTree 方法读取XML
来源:互联网 发布:法比奥奥奥 知乎 编辑:程序博客网 时间:2024/05/15 02:18
要想解析XML需分两步:
第一步: 加载XML文件
1) 加载指定字符串
2) 加载指定文件
第二步: 获取节点的方法
1) 通过 getiterator
2) 通过 getchildren
3) find 方法
4) findall 方法
#coding=GBKfrom xml.etree import ElementTree as ETdef print_node(node): print "==============================================" print "node.attrib:%s" % node.attrib print "node.tag:%s" % node.tag print "node.text:%s" % node.text if node.attrib.has_key("name"): print "node.attrib['rank']:%s" % node.attrib['name'] if __name__ == '__main__':#第一步: 加载XML文件 #1.从变量读取,参数为XML段,返回的是一个根Element对象 root = ET.fromstring(open("test.xml").read()) print (type(root)) #打印结果: <class 'xml.etree.ElementTree.Element'> #2.从xml文件中读取,用getroot获取根节点,根节点也是Element对象 tree = ET.parse(r'test.xml') root2 = tree.getroot() print (type(root2)) #打印结果: <class 'xml.etree.ElementTree.Element'> #第二步: 获取节点的方法 # 1.通过getiterator print('1.通过getiterator ') first_node = root.getiterator("country") for node in first_node: print_node(node) #获取指定某个节点(这里是"country") 个数 print first_node.__len__() # 2.通过 getchildren获取指定某个节点的子节点 print ('2.通过 getchildren获取指定某个节点的子节点') first_node_child = first_node[0].getchildren()[0] print_node(first_node_child) print first_node[0].getchildren()[1], first_node[0].getchildren()[1].tag # 3.find方法 ,返回第一个找到的节点 print ('3.find方法,返回第一个找到的节点 ') node_find = root.find('country2') print node_find, node_find.attrib, '==' print_node(node_find) # 4.findall方法 print ('4.findall方法,返回一个所有找到的节点的元组 ') node_findall = root.findall("country/rank") print node_findall
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> <country2 name="Test"> <rank>234</rank> <year>2016</year> <gdppc>346344</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country2> </data>
阅读全文
0 0
- Python学习-ElementTree 方法读取XML
- 【python】xml Elementtree 学习一
- Python模块学习之xml.etree.ElementTree
- Python读取xml文件(xml.etree.ElementTree)
- python xml解析ElementTree
- python解析xml ElementTree
- python学习之 -- xml.etree.ElementTree解析xml
- python学习之os.path模块&&elementtree处理xml文件
- Python使用ElementTree解析XML
- python 使用ElementTree解析XML
- python操作XML文件------elementtree
- Python 使用ElementTree解析XML
- Python中用ElementTree解析XML
- python xml.etree.ElementTree解析
- Python xml.etree.ElementTree 用法
- Python中用ElementTree解析XML
- 使用 Python ElementTree 生成 xml
- Python使用ElementTree处理XML
- 逻辑回归到底是线性的还是非线性的?
- Java开发小技巧(一)
- CSDN博客排名规则和积分获取方法
- 构建云上企业数据库架构分为哪五步?
- Java小功能(3)--将字符串写入文件
- Python学习-ElementTree 方法读取XML
- redis集群2
- 深度学习基础之LSTM
- BASIC-20VIP试题 数的读法
- typora+pandoc:markdown文本转换成word(也可把word转换成markdown哦)
- Django框架学习笔记(17.多对多简易实例)
- navicat执行sql文件报错
- GCD介绍及线程队列的使用
- java-17(2)-字符流缓冲区