[Tutor] Search and replace text in XML file?

Bod Soutar bodsda at googlemail.com
Sat Jul 28 09:22:18 CEST 2012


On Jul 28, 2012 2:39 AM, "Todd Tabern" <TTabern at ddti.net> wrote:
>
> I'm looking to search an entire XML file for specific text and replace
that text, while maintaining the structure of the XML file. The text occurs
within multiple nodes throughout the file.
> I basically need to replace every occurrence C:\Program Files with
C:\Program Files (x86), regardless of location. For example, that text
appears within:
> <URL>C:\Program Files\\Map Data\Road_Centerlines.shp</URL>
> and also within:
> <RoutingIndexPathName>C:\Program
Files\Templates\RoadNetwork.rtx</RoutingIndexPathName>
> ...among others.
> I've tried some non-python methods and they all ruined the XML structure.
I've been Google searching all day and can only seem to find solutions that
look for a specific node and replace the whole string between the tags.
> I've been looking at using minidom to achieve this but I just can't seem
to figure out the right method.
> My end goal, once I have working code, is to compile an exe that can work
on machines without python, allowing a user can click in order to perform
the XML modification.
> Thanks in advance.

I'm not sure what you have tried already, but this should be as simple as
reading the file, replacing the strings and then writing the file. Because
you don't care about just a specific entry, you don't really need to be
concerned that its an XML file.

If you continue having formatting problems, send us a sample and the code
you've tried.

Bodsda
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120728/df1349d5/attachment-0001.html>


More information about the Tutor mailing list