python之xml 文件的读取方法

'''
xml 文件的读取方法
'''
#!/usr/bin/env python
# -*- coding: utf- -*- import xml.etree.ElementTree as ET from datetime import datetime
tree = ET.parse("country.xml")
root = tree.getroot()
print "**************tag 根元素*********"
print(root.tag, ":", root.attrib) # 打印根元素的tag和属性
# find('nodeName'):表示在该节点下,查找其中第一个tag为nodeName的节点。
print "*#查找root节点下第一个tag为country的节点********"
animNode = root.find('country') #查找root节点下第一个tag为country的节点
print(animNode.tag,animNode.attrib,animNode.text)
print "**查找其中所有tag为nodeName的节点*****"
# findall('nodeName'):表示在该节点下,查找其中所有tag为nodeName的节点
# animNode = root.findall('country') #查找root节点下所有 tag为country的节点
# print(animNode.tag,animNode.attrib,animNode.text) print "*删除指定的节点的信息 保存文件*"
animNode = root.find('country')
if animNode.attrib['name'] == 'Liechtenstein':
root.remove(animNode)
tree.write('finish.xml') # 保存修改后的XML文件 cur_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print "current time: ",cur_time
# 遍历xml文档的第二层
for child in root:
# 第二层节点的标签名称和属性
print(child.tag,":", child.attrib)
# 遍历xml文档的第三层
for children in child:
# 第三层节点的标签名称和属性
print(children.tag, ":", children.attrib)
上一篇:使用sqlldr命令导入资料到Oracle数据库表中的示例说明


下一篇:【VS开发】【智能语音处理】DTW算法(语音识别)