23 lines
649 B
Python
23 lines
649 B
Python
|
from xml.etree.ElementTree import parse, Element
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
doc = parse(r'6.数据编码与处理/6.test.xml')
|
||
|
# 得到XML的root
|
||
|
root = doc.getroot()
|
||
|
print(root)
|
||
|
|
||
|
# 从root里移除一些从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)
|
||
|
|
||
|
# 保存为XML文件
|
||
|
doc.write(r'6.数据编码与处理/6.test_result.xml', xml_declaration=True)
|