每天CookBook之Python-99

  • 解析、修改和重写xml文件
from xml.etree.ElementTree import parse, Element

doc = parse('pred.xml')
root = doc.getroot()

print(root)

root.remove(root.find('sri'))
root.remove(root.find('cr'))

print(root.getchildren().index(root.find('nm')))

e = Element('spam')
e.text = 'This is a test'
root.insert(2, e)

doc.write('newpred.xml', xml_declaration=True)
<Element 'stop' at 0x005BE6C0>
1
posted @ 2016-07-27 23:51  4Thing  阅读(77)  评论(0编辑  收藏  举报