如何使用Python修改XML文件?

2024-05-23 13:37:02 发布

您现在位置:Python中文网/ 问答频道 /正文

实际上,我已经得到了XML字符串并解析了该字符串以从中获取属性。现在我想更改我的XML文件,查看属性。就像我想改变笔划的颜色。有什么办法吗?我将如何更改并再次保存文件。在

import requests
from xml.dom import minidom

response = requests.get('http://localhost:8080/geoserver/rest/styles/pakistan.sld',
                        auth=('admin', 'geoserver'))
fo=open("/home/adeel/Desktop/untitled1/yes.xml", "wb")
fo.write(response.text)
fo.close()

xmldoc = minidom.parse('yes.xml')
itemlist = xmldoc.getElementsByTagName('CssParameter')
print "Len : ", len(itemlist)
#print "Attribute Name : ", \
itemlist[0].attributes['name'].value
print "Text : ", itemlist[0].firstChild.nodeValue

for s in itemlist :
    print "Attribute Name : ", s.attributes['name'].value
    print "Text : ", s.firstChild.nodeValue

Tags: 文件字符串import属性responsegeoserverxmlrequests