您的位置:首页 > 编程语言 > Python开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: