Thanks for the tip. I can write the changed data back to my xml file. One snag that I found is that the des encryption that I used for the data that is written back, it is not parsed correctly when the file is read again with the new data in it. There is non-printable characters or non-ascii chars in that gives errors from expat when the contents is parsed.
I had to use a different encryption algorithm. I am going to do some tests on it now.
Johan
On Tue, 2005-08-30 at 09:47 -0400, Kent Johnson wrote:
Johan Geldenhuys wrote: > That means that I have to compile the whole file from scratch in Python, > minidom. > I am not that good, yet, but will try. No, not if I understand what you are trying to do - the xmlDocument you have is all the data from the file, just write it back out using the code I posted before. > will it be easier to search for the string that I look for in the file > (readlines) and then just write the pieces back again? That depends a lot on the data. If you can reliably find what you want by looking at a line at a time, that is a simple approach. But you are almost there with the minidom. Really, just add my three lines of code to what you already have. (Maybe for prudence use a different file name.) Kent > > Johan > On Tue, 2005-08-30 at 07:40 -0400, Kent Johnson wrote: > >>Johan Geldenhuys wrote: >>> Thanks for he help, so far. >>> >>> 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? >> >>The minidom docs say that DOM objects have a writexml() method: >>writexml(writer[,indent=""[,addindent=""[,newl=""]]]) >> Write XML to the writer object. The writer should have a write() method which matches that of the file object interface. >> >>This is saying that 'writer' should act like a file object. So it can just be a file object obtained by calling open(). In other words something like >>f = open(file_name, 'w') >>xmlDocument.writexml(f) >>f.close() >> >>should do it. If you have non-ascii characters in your XML you should use codecs.open() instead of plain open() and encode your XML as desired (probably utf-8). >> >>> Do I have to write all the data back or can I just replace the pieces I >>> changed? >> >>You have to write it all back. >> >>Kent >> >>_______________________________________________ >>Tutor maillist - Tutor@python.org <mailto:Tutor@python.org> >>http://mail.python.org/mailman/listinfo/tutor >> > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
|
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor