Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26153

Re: Search and replace text in XML file?

References <5535f70f-4542-451f-8b08-a62b45d15c11@googlegroups.com>
Date 2012-07-27 20:07 -0600
Subject Re: Search and replace text in XML file?
From Jason Friedman <jason@powerpull.net>
Newsgroups comp.lang.python
Message-ID <mailman.2665.1343441271.4697.python-list@python.org> (permalink)

Show all headers | View raw


> 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.

Is it as simple as this?

$ cat my.xml
<URL>C:\Program Files\\Map Data\Road_Centerlines.shp</URL>
and also within:
<RoutingIndexPathName>C:\Program
Files\Templates\RoadNetwork.rtx</RoutingIndexPathName>

$ python3
Python 3.2.3 (default, May  3 2012, 15:51:42)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> for line in open("my.xml"):
...     print(line.replace("C:\Program Files", "C:\Program Files
(x86)"), end="")
...
<URL>C:\Program Files (x86)\\Map Data\Road_Centerlines.shp</URL>
and also within:
<RoutingIndexPathName>C:\Program Files
(x86)\Templates\RoadNetwork.rtx</RoutingIndexPathName>

This solution assumes that you have no tags containing the string
"C:\Program Files".

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Search and replace text in XML file? todd.tabern@gmail.com - 2012-07-27 18:23 -0700
  Re: Search and replace text in XML file? Jason Friedman <jason@powerpull.net> - 2012-07-27 20:07 -0600
  Re: Search and replace text in XML file? Terry Reedy <tjreedy@udel.edu> - 2012-07-28 03:17 -0400
    Re: Search and replace text in XML file? Paul Rudin <paul.nospam@rudin.co.uk> - 2012-07-31 15:45 +0100
  Re: Search and replace text in XML file? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-07-28 09:29 +0100
  Re: Search and replace text in XML file? MRAB <python@mrabarnett.plus.com> - 2012-07-28 17:01 +0100
  Re: Search and replace text in XML file? Tim Roberts <timr@probo.com> - 2012-07-28 15:32 -0700

csiph-web