Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Search and replace text in XML file? (http://www.velocityreviews.com/forums/t948628-search-and-replace-text-in-xml-file.html)

todd.tabern@gmail.com 07-28-2012 01:23 AM

Search and replace text in XML file?
 
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.

Jason Friedman 07-28-2012 02:07 AM

Re: Search and replace text in XML file?
 
> 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".

Terry Reedy 07-28-2012 07:17 AM

Re: Search and replace text in XML file?
 
On 7/27/2012 9:23 PM, todd.tabern@gmail.com 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.


For a one-off project, or for experimentation, I would use a proper
text-editor* and run through with search/replace. For automation, use
Jason's suggestion of .replace, perhaps on a line-by-line basis.

* By definition, such only make changes you request. Notepad++ is one
such on Windows. You do not want a word or document processor, or, for
simple xml oblivious text substitutions, an xml processor. Such things
often make changes of various sorts.

--
Terry Jan Reedy




Mark Lawrence 07-28-2012 08:29 AM

Re: Search and replace text in XML file?
 
On 28/07/2012 08:17, Terry Reedy wrote:
> On 7/27/2012 9:23 PM, todd.tabern@gmail.com 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.

>
> For a one-off project, or for experimentation, I would use a proper
> text-editor* and run through with search/replace. For automation, use
> Jason's suggestion of .replace, perhaps on a line-by-line basis.
>
> * By definition, such only make changes you request. Notepad++ is one
> such on Windows. You do not want a word or document processor, or, for
> simple xml oblivious text substitutions, an xml processor. Such things
> often make changes of various sorts.
>


I highly recommend the use of notepad++. If anyone knows of a better
text editor for Windows please let me know :)

--
Cheers.

Mark Lawrence.


MRAB 07-28-2012 04:01 PM

Re: Search and replace text in XML file?
 
On 28/07/2012 09:29, Mark Lawrence wrote:
> On 28/07/2012 08:17, Terry Reedy wrote:
>> On 7/27/2012 9:23 PM, todd.tabern@gmail.com 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.

>>
>> For a one-off project, or for experimentation, I would use a proper
>> text-editor* and run through with search/replace. For automation, use
>> Jason's suggestion of .replace, perhaps on a line-by-line basis.
>>
>> * By definition, such only make changes you request. Notepad++ is one
>> such on Windows. You do not want a word or document processor, or, for
>> simple xml oblivious text substitutions, an xml processor. Such things
>> often make changes of various sorts.
>>

>
> I highly recommend the use of notepad++. If anyone knows of a better
> text editor for Windows please let me know :)
>

My own preference is EditPad. I liked EditPad Lite so much that I
bought EditPad Pro.

Paul Rudin 07-31-2012 02:45 PM

Re: Search and replace text in XML file?
 
Terry Reedy <tjreedy@udel.edu> writes:

> ... a proper text-editor*


> * ... Notepad++ is one such on Windows.


Surely emacs is the only such on any platform? :)



All times are GMT. The time now is 08:45 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.