利用 python 的 ElementTree 编辑 XML 文件

来源:互联网 发布:知乎的神回复 编辑:程序博客网 时间:2024/06/06 21:44
# coding:UTF-8

import xml.etree.ElementTree as ET
from xml.etree.ElementTree import SubElement

with open('server.xml', 'rt') as f:
    tree = ET.parse(f)

root = tree.getroot()

print root.attrib

lst = tree.findall('Service/Engine/Realm/Realm')
print lst

for e in lst:
    if e.attrib['className'].startswith('org'):
        e.attrib['className'] = 'org.xxx'
print ET.tostring(root)
原创粉丝点击