python解析xml文件
2016-03-05 20:11
696 查看
ET.find(path[, namespaces=D])
This method is used to find a specific single element in the document. It is essentially equivalent to calling the
.find()method on the document's root element;
<country name="chinese">
>>> for child in root: ... print child.tag, child.attrib
child.tag 是标签项(country), child.attrib 是标签的名字(chinese)
root = ElementTree.fromstring(text)
# 获取element的方法
# 1 通过getiterator
infra_node = root.getiterator("virtual-infrastructure")
#for node in lst_node:
# print_node(node)
# 2通过 getchildren Deprecated since version 2.7: Use
list(elem)or
iteration
lst_node_child = infra_node[0].getchildren()[0]
print_node(lst_node_child)
# 3 .find方法
node_find = root.find('address')
#print_node(node_find)
#4. findall方法
node_findall = root.findall("virtual-infrastructure/vm")[0]
#print_node(node_findall)
for elem in tree.iter(tag='branch'): ... print elem.tag, elem.attrib
相关文章推荐
- Python的单引号、双引号和三引号的字符串
- python安装mysqldb
- python基础(List,Dictionary,Random,循环,Lambda,Tumple)
- python 操作注册表
- Python批量重命名
- python and or and-or
- python爬虫方法小结
- python面试题
- 编程学习第二天,初识Python
- python 小练习之山寨版markdown格式txt文件转html文件
- Python爬虫爬取一篇韩寒新浪博客
- 客户端python热更新
- python基础0305
- “笨办法学python”学习笔记-习题5
- python 第三方库下载地址
- python 数据类型
- Python
- python GUI实践:做一个满图乱跑的小球
- Python xml解析记录
- Python基础记录