<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<META NAME="GENERATOR" CONTENT="GtkHTML/3.6.1">
</HEAD>
<BODY>
Kent,<BR>
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.<BR>
I had to use a different encryption algorithm. I am going to do some tests on it now.<BR>
<BR>
Johan<BR>
On Tue, 2005-08-30 at 09:47 -0400, Kent Johnson wrote:
<BLOCKQUOTE TYPE=CITE>
<PRE>
<FONT COLOR="#000000">Johan Geldenhuys wrote:</FONT>
<FONT COLOR="#000000">> That means that I have to compile the whole file from scratch in Python, </FONT>
<FONT COLOR="#000000">> minidom.</FONT>
<FONT COLOR="#000000">> I am not that good, yet, but will try.</FONT>
<FONT COLOR="#000000">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.</FONT>
<FONT COLOR="#000000">> will it be easier to search for the string that I look for in the file </FONT>
<FONT COLOR="#000000">> (readlines) and then just write the pieces back again?</FONT>
<FONT COLOR="#000000">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.)</FONT>
<FONT COLOR="#000000">Kent</FONT>
<FONT COLOR="#000000">> </FONT>
<FONT COLOR="#000000">> Johan</FONT>
<FONT COLOR="#000000">> On Tue, 2005-08-30 at 07:40 -0400, Kent Johnson wrote:</FONT>
<FONT COLOR="#000000">> </FONT>
<FONT COLOR="#000000">>>Johan Geldenhuys wrote:</FONT>
<FONT COLOR="#000000">>>> Thanks for he help, so far.</FONT>
<FONT COLOR="#000000">>>> </FONT>
<FONT COLOR="#000000">>>> I am still having some questions on writing my new string back to the </FONT>
<FONT COLOR="#000000">>>> xml file after I found what I was looking for and changed it.</FONT>
<FONT COLOR="#000000">>>> </FONT>
<FONT COLOR="#000000">>>> Extracts:</FONT>
<FONT COLOR="#000000">>>> </FONT>
<FONT COLOR="#000000">>>> xmlDocument = minidom.parse(file_name) # open existing file for parsing</FONT>
<FONT COLOR="#000000">>>> main = xmlDocument.getElementsByTagName('Config')</FONT>
<FONT COLOR="#000000">>>> main.getElementsByTagName('Connection')</FONT>
<FONT COLOR="#000000">>>> configSection = mainSection[0]</FONT>
<FONT COLOR="#000000">>>> </FONT>
<FONT COLOR="#000000">>>> for node in configSection: #Here I get the NamedNodeMap info</FONT>
<FONT COLOR="#000000">>>> password = node.getAttribute("password")</FONT>
<FONT COLOR="#000000">>>> # Do stuff to the password and I have 'newPass'</FONT>
<FONT COLOR="#000000">>>> node.removeAttribute('password') # I take out my old attribute </FONT>
<FONT COLOR="#000000">>>> and it's value</FONT>
<FONT COLOR="#000000">>>> node.setAttribute('password', newPass)</FONT>
<FONT COLOR="#000000">>>> </FONT>
<FONT COLOR="#000000">>>> </FONT>
<FONT COLOR="#000000">>>> At this stage I have my new attribute and it's new value, but how do I </FONT>
<FONT COLOR="#000000">>>> write that to my file in the same place?</FONT>
<FONT COLOR="#000000">>>> I have to get a 'writer', how do I do this?</FONT>
<FONT COLOR="#000000">>></FONT>
<FONT COLOR="#000000">>>The minidom docs say that DOM objects have a writexml() method:</FONT>
<FONT COLOR="#000000">>>writexml(writer[,indent=""[,addindent=""[,newl=""]]])</FONT>
<FONT COLOR="#000000">>> Write XML to the writer object. The writer should have a write() method which matches that of the file object interface.</FONT>
<FONT COLOR="#000000">>></FONT>
<FONT COLOR="#000000">>>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</FONT>
<FONT COLOR="#000000">>>f = open(file_name, 'w')</FONT>
<FONT COLOR="#000000">>>xmlDocument.writexml(f)</FONT>
<FONT COLOR="#000000">>>f.close()</FONT>
<FONT COLOR="#000000">>></FONT>
<FONT COLOR="#000000">>>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).</FONT>
<FONT COLOR="#000000">>></FONT>
<FONT COLOR="#000000">>>> Do I have to write all the data back or can I just replace the pieces I </FONT>
<FONT COLOR="#000000">>>> changed?</FONT>
<FONT COLOR="#000000">>></FONT>
<FONT COLOR="#000000">>>You have to write it all back.</FONT>
<FONT COLOR="#000000">>></FONT>
<FONT COLOR="#000000">>>Kent</FONT>
<FONT COLOR="#000000">>></FONT>
<FONT COLOR="#000000">>>_______________________________________________</FONT>
<FONT COLOR="#000000">>>Tutor maillist - <A HREF="mailto:Tutor@python.org">Tutor@python.org</A> <mailto:<A HREF="mailto:Tutor@python.org">Tutor@python.org</A>></FONT>
<FONT COLOR="#000000">>><A HREF="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</A></FONT>
<FONT COLOR="#000000">>></FONT>
<FONT COLOR="#000000">> </FONT>
<FONT COLOR="#000000">_______________________________________________</FONT>
<FONT COLOR="#000000">Tutor maillist - <A HREF="mailto:Tutor@python.org">Tutor@python.org</A></FONT>
<FONT COLOR="#000000"><A HREF="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</A></FONT>
</PRE>
</BLOCKQUOTE>
<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<BR>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>