简体   繁体   中英

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. Now I want my XML file to change, viewing the attributes. 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. Once you've changed it you need to make a PUT request to place the file back on the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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