简体   繁体   English

如何使用Python修改XML文件?

[英]How to modify the XML file using Python?

Actually I have got the XML string and parse the string to get the attributes from it. 实际上,我已经获得了XML字符串并解析了该字符串以从中获取属性。 Now I want my XML file to change, viewing the attributes. 现在,我希望更改XML文件,同时查看属性。 Like I want to change the color of the stroke. 就像我要更改笔触的颜色一样。 Is there any way? 有什么办法吗? How I will change and then again save the file. 我将如何更改,然后再次保存文件。

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

You should probably read through the SLD Cook book to get hints on how to change things like the colour of the lines in your SLD. 您可能应该通读SLD Cook书,以获得有关如何更改诸如SLD中线条颜色之类的提示。 Once you've changed it you need to make a PUT request to place the file back on the server. 更改后,您需要发出一个PUT请求 ,将文件放回到服务器上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM