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?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <jsf80238@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'method.': 0.05; 'python3': 0.05; 'subject:text': 0.05; 'subject:file': 0.07; 'python': 0.09; 'skip:f 30': 0.15; 'exe': 0.16; 'goal,': 0.16; 'nodes': 0.16; 'non-python': 0.16; 'structure.': 0.16; 'subject:XML': 0.16; 'tags.': 0.16; 'string': 0.17; 'basically': 0.17; '>>>': 0.18; 'appears': 0.18; 'code,': 0.18; 'sender:addr:gmail.com': 0.18; 'skip:p 30': 0.20; 'file.': 0.20; 'regardless': 0.21; 'assumes': 0.22; 'occurs': 0.22; "i've": 0.23; 'tried': 0.25; 'header:In-Reply-To:1': 0.25; 'replace': 0.27; 'message-id:@mail.gmail.com': 0.27; 'this?': 0.28; 'cat': 0.29; 'node': 0.29; "i'm": 0.29; 'figure': 0.30; 'file': 0.32; 'received:209.85.160.46': 0.32; 'structure': 0.32; 'text,': 0.33; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; "can't": 0.34; 'received:google.com': 0.34; 'text': 0.34; 'machines': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'others.': 0.36; 'xml': 0.37; 'maintaining': 0.37; 'throughout': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'perform': 0.38; 'skip:( 30': 0.38; 'files': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'google': 0.39; 'header:Received:5': 0.40; 'end': 0.40; 'containing': 0.61; 'between': 0.63; 'more': 0.63; 'within': 0.64; 'day': 0.73; 'click': 0.76; 'subject:Search': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:content-type; bh=Rm4GI67j3FgvhfcLbSNs4FedktArzI9FF5md5ucTrLI=; b=u4nK8QrHcbGg4buGS6e5D4DxIRy02TXv1hbTumeJu9b+ezRR3XRHJuDaDFtAmKetrI r/LOnat+/4RjvAUhWbXmSu6KK58wgUHT5aCkjLLAS4sPWpcYNZhAl+VaTmApOFkquh3w 2hJvNRzOZMnd5oGZebB0x9C7Dr3RT7URsgk1epinU4m9RnvGSTro4ToQg2CEkYEgFbM6 1QjSChiBEh926Os0A8d23EgrkyXUH4D14sP28ql+m7ya3kvcpdqs8KpcQ73cj6zAeiSj CRX2gjWe9QQwt3XS5d8o1hTENN9y8rBBxbDs2XwXB95X8dJlsnpuEPYm3ACGp5mI4YZZ iPGg==
MIME-Version 1.0
Sender jsf80238@gmail.com
In-Reply-To <5535f70f-4542-451f-8b08-a62b45d15c11@googlegroups.com>
References <5535f70f-4542-451f-8b08-a62b45d15c11@googlegroups.com>
Date Fri, 27 Jul 2012 20:07:48 -0600
X-Google-Sender-Auth Kq3UGHJ5w2qST5SCWiBd0Wg6cvg
Subject Re: Search and replace text in XML file?
From Jason Friedman <jason@powerpull.net>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2665.1343441271.4697.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1343441271 news.xs4all.nl 6876 [2001:888:2000:d::a6]:45999
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:26153

Show key headers only | 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