I am still having some questions on writing my new string back to the xml file after I found what I was looking for and changed it.
Extracts:
xmlDocument = minidom.parse(file_name) # open existing file for parsing
main = xmlDocument.getElementsByTagName('Config')
main.getElementsByTagName('Connection')
configSection = mainSection[0]
for node in configSection: #Here I get the NamedNodeMap info
password = node.getAttribute("password")
# Do stuff to the password and I have 'newPass'
node.removeAttribute('password') # I take out my old attribute and it's value
node.setAttribute('password', newPass)
At this stage I have my new attribute and it's new value, but how do I write that to my file in the same place?
I have to get a 'writer', how do I do this?
Do I have to write all the data back or can I just replace the pieces I changed?
Thanks,
|
--- Begin Message ---> """ Parse the xml file """ > xmlDocument = minidom.parse(self.configFile) [code cut]> Now I want to change a string that a retrieved from the file and write > it back to where it was. So, I get something, change it and write it > back. > > How do I put the new string in the place of the old? How do I overwrite > the first value with the new value? Hi Johan, The documentation in: http://www.python.org/doc/lib/module-xml.dom.minidom.html has a small example where they insert text into an element: ###### (From the documentation) from xml.dom.minidom import getDOMImplementation impl = getDOMImplementation() newdoc = impl.createDocument(None, "some_tag", None) top_element = newdoc.documentElement text = newdoc.createTextNode('Some textual content.') top_element.appendChild(text) ###### Elements have methods like appendChild(), replaceChild() and removeChild(). So it should be fairly straightforward to replace the existing text node with a new one. That being said, the DOM model is a bit verbose and feels very low-level. Have you looked at the third-party "ElementTree" module yet? http://effbot.org/zone/element-index.htm It's a bit more convenient to work with; its model maps better to Python. Good luck! -- This E-Mail has been scanned. Enjoy Your Day.
--- End Message ---
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
