<!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">&gt; That means that I have to compile the whole file from scratch in Python, </FONT>
<FONT COLOR="#000000">&gt; minidom.</FONT>
<FONT COLOR="#000000">&gt; 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">&gt; will it be easier to search for the string that I look for in the file </FONT>
<FONT COLOR="#000000">&gt; (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">&gt; </FONT>
<FONT COLOR="#000000">&gt; Johan</FONT>
<FONT COLOR="#000000">&gt; On Tue, 2005-08-30 at 07:40 -0400, Kent Johnson wrote:</FONT>
<FONT COLOR="#000000">&gt; </FONT>
<FONT COLOR="#000000">&gt;&gt;Johan Geldenhuys wrote:</FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; Thanks for he help, so far.</FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; </FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; I am still having some questions on writing my new string back to the </FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; xml file after I found what I was looking for and changed it.</FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; </FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; Extracts:</FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; </FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; xmlDocument = minidom.parse(file_name) # open existing file for parsing</FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; main = xmlDocument.getElementsByTagName('Config')</FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; main.getElementsByTagName('Connection')</FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; configSection = mainSection[0]</FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; </FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; for node in configSection: #Here I get the NamedNodeMap info</FONT>
<FONT COLOR="#000000">&gt;&gt;&gt;         password = node.getAttribute(&quot;password&quot;)</FONT>
<FONT COLOR="#000000">&gt;&gt;&gt;         # Do stuff to the password and I have 'newPass'</FONT>
<FONT COLOR="#000000">&gt;&gt;&gt;        node.removeAttribute('password') # I take out my old attribute </FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; and it's value</FONT>
<FONT COLOR="#000000">&gt;&gt;&gt;        node.setAttribute('password', newPass)</FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; </FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; </FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; At this stage I have my new attribute and it's new value, but how do I </FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; write that to my file in the same place?</FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; I have to get a 'writer', how do I do this?</FONT>
<FONT COLOR="#000000">&gt;&gt;</FONT>
<FONT COLOR="#000000">&gt;&gt;The minidom docs say that DOM objects have a writexml() method:</FONT>
<FONT COLOR="#000000">&gt;&gt;writexml(writer[,indent=&quot;&quot;[,addindent=&quot;&quot;[,newl=&quot;&quot;]]])</FONT>
<FONT COLOR="#000000">&gt;&gt;    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">&gt;&gt;</FONT>
<FONT COLOR="#000000">&gt;&gt;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">&gt;&gt;f = open(file_name, 'w')</FONT>
<FONT COLOR="#000000">&gt;&gt;xmlDocument.writexml(f)</FONT>
<FONT COLOR="#000000">&gt;&gt;f.close()</FONT>
<FONT COLOR="#000000">&gt;&gt;</FONT>
<FONT COLOR="#000000">&gt;&gt;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">&gt;&gt;</FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; Do I have to write all the data back or can I just replace the pieces I </FONT>
<FONT COLOR="#000000">&gt;&gt;&gt; changed?</FONT>
<FONT COLOR="#000000">&gt;&gt;</FONT>
<FONT COLOR="#000000">&gt;&gt;You have to write it all back.</FONT>
<FONT COLOR="#000000">&gt;&gt;</FONT>
<FONT COLOR="#000000">&gt;&gt;Kent</FONT>
<FONT COLOR="#000000">&gt;&gt;</FONT>
<FONT COLOR="#000000">&gt;&gt;_______________________________________________</FONT>
<FONT COLOR="#000000">&gt;&gt;Tutor maillist  -  <A HREF="mailto:Tutor@python.org">Tutor@python.org</A> &lt;mailto:<A HREF="mailto:Tutor@python.org">Tutor@python.org</A>&gt;</FONT>
<FONT COLOR="#000000">&gt;&gt;<A HREF="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</A></FONT>
<FONT COLOR="#000000">&gt;&gt;</FONT>
<FONT COLOR="#000000">&gt; </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>