By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,996 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,996 IT Pros & Developers. It's quick & easy.

replacing string in xml file--revisited

P: n/a
Hi,
I need to replace a string in xml file with something else.Ex

- <SERVICEPARAMETER id="_775" Semantics="subfunction" DDORef="_54">
<SHORTNAME>rate</SHORTNAME>
<LONGNAME>rate</LONGNAME>
<VALUE role="constant" DataType="unsigned" value="1" />
<BYTEPOSITION role="position" BytePos="1" />
</SERVICEPARAMETER>
- <SERVICEPARAMETER id="_776" Semantics="localId" DDORef="_54">
Here i have opened an xml
file(small part is pasted here).I want to replace the word 'localId'
with 'dataPackageID' wherever it comes in xml file.I have asked this
before and got a code:
input_file = open(filename)
xmlcontents = input_file.read()
input_file.close()
xmlcontents = xmlcontents.replace("spam", "eggs")
output_file = open(filename,"w")
output_file.write(xmlcontents)
output_file.close()

Although this works alone it is nto
working when i handle multiple file I/O.Is there a alternative to do
this.(maybe without read() operation)
Thanks

May 10 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
In <11*********************@w5g2000hsg.googlegroups.c om>, saif.shakeel
wrote:
Although this works alone it is nto
working when i handle multiple file I/O.Is there a alternative to do
this.(maybe without read() operation)
Why do you want to change the part that *works* instead of fixing the code
that doesn't!?

Ciao,
Marc 'BlackJack' Rintsch
May 10 '07 #2

P: n/a
On May 10, 12:56 am, saif.shak...@gmail.com wrote:
Hi,
I need to replace a string in xml file with something else.Ex

- <SERVICEPARAMETER id="_775" Semantics="subfunction" DDORef="_54">
<SHORTNAME>rate</SHORTNAME>
<LONGNAME>rate</LONGNAME>
<VALUE role="constant" DataType="unsigned" value="1" />
<BYTEPOSITION role="position" BytePos="1" />
</SERVICEPARAMETER>
- <SERVICEPARAMETER id="_776" Semantics="localId" DDORef="_54">

Here i have opened an xml
file(small part is pasted here).I want to replace the word 'localId'
with 'dataPackageID' wherever it comes in xml file.I have asked this
before and got a code:
input_file = open(filename)
xmlcontents = input_file.read()
input_file.close()
xmlcontents = xmlcontents.replace("spam", "eggs")
output_file = open(filename,"w")
output_file.write(xmlcontents)
output_file.close()

Although this works alone it is nto
working when i handle multiple file I/O.Is there a alternative to do
this.(maybe without read() operation)
Thanks
try this...

#!/usr/bin/env python

from elementtree import ElementTree as et
tree = et.parse("testxml.xml")

for t in tree.getiterator("SERVICEPARAMETER"):
t.set("Semantics", "localId")

tree.write("output.xml")

~Sean

May 10 '07 #3

P: n/a
On May 10, 12:56 am, saif.shak...@gmail.com wrote:
Hi,
I need to replace a string in xml file with something else.Ex

- <SERVICEPARAMETER id="_775" Semantics="subfunction" DDORef="_54">
<SHORTNAME>rate</SHORTNAME>
<LONGNAME>rate</LONGNAME>
<VALUE role="constant" DataType="unsigned" value="1" />
<BYTEPOSITION role="position" BytePos="1" />
</SERVICEPARAMETER>
- <SERVICEPARAMETER id="_776" Semantics="localId" DDORef="_54">

Here i have opened an xml
file(small part is pasted here).I want to replace the word 'localId'
with 'dataPackageID' wherever it comes in xml file.I have asked this
before and got a code:
input_file = open(filename)
xmlcontents = input_file.read()
input_file.close()
xmlcontents = xmlcontents.replace("spam", "eggs")
output_file = open(filename,"w")
output_file.write(xmlcontents)
output_file.close()

Although this works alone it is nto
working when i handle multiple file I/O.Is there a alternative to do
this.(maybe without read() operation)
Thanks
After reading your post again, this might be better:

#!/usr/bin/env python

from elementtree import ElementTree as et
tree = et.parse("testxml.xml")

for t in tree.getiterator("SERVICEPARAMETER"):
if t.get("Semantics") == "localId":
t.set("Semantics", "dataPackageID")

tree.write("output.xml")

~Sean

May 10 '07 #4

P: n/a
On May 10, 1:42 pm, half.ital...@gmail.com wrote:
On May 10, 12:56 am, saif.shak...@gmail.com wrote:


Hi,
I need to replace a string in xml file with something else.Ex
- <SERVICEPARAMETER id="_775" Semantics="subfunction" DDORef="_54">
<SHORTNAME>rate</SHORTNAME>
<LONGNAME>rate</LONGNAME>
<VALUE role="constant" DataType="unsigned" value="1" />
<BYTEPOSITION role="position" BytePos="1" />
</SERVICEPARAMETER>
- <SERVICEPARAMETER id="_776" Semantics="localId" DDORef="_54">
Here i have opened an xml
file(small part is pasted here).I want to replace the word 'localId'
with 'dataPackageID' wherever it comes in xml file.I have asked this
before and got a code:
input_file = open(filename)
xmlcontents = input_file.read()
input_file.close()
xmlcontents = xmlcontents.replace("spam", "eggs")
output_file = open(filename,"w")
output_file.write(xmlcontents)
output_file.close()
Although this works alone it is nto
working when i handle multiple file I/O.Is there a alternative to do
this.(maybe without read() operation)
Thanks

try this...

#!/usr/bin/env python

from elementtree import ElementTree as et
tree = et.parse("testxml.xml")

for t in tree.getiterator("SERVICEPARAMETER"):
t.set("Semantics", "localId")

tree.write("output.xml")

~Sean- Hide quoted text -

- Show quoted text -
#!/usr/bin/env python
from elementtree import ElementTree as et
tree = et.parse("testxml.xml")
for t in tree.getiterator("SERVICEPARAMETER"):
t.set("Semantics", "localId")
tree.write("output.xml")
Is this code
complete,where are you replacing the localid with "datapackageid",and
where is the new xml being stored.
Thanks for the replies

May 10 '07 #5

P: n/a
On May 10, 1:55 pm, half.ital...@gmail.com wrote:
On May 10, 12:56 am, saif.shak...@gmail.com wrote:


Hi,
I need to replace a string in xml file with something else.Ex
- <SERVICEPARAMETER id="_775" Semantics="subfunction" DDORef="_54">
<SHORTNAME>rate</SHORTNAME>
<LONGNAME>rate</LONGNAME>
<VALUE role="constant" DataType="unsigned" value="1" />
<BYTEPOSITION role="position" BytePos="1" />
</SERVICEPARAMETER>
- <SERVICEPARAMETER id="_776" Semantics="localId" DDORef="_54">
Here i have opened an xml
file(small part is pasted here).I want to replace the word 'localId'
with 'dataPackageID' wherever it comes in xml file.I have asked this
before and got a code:
input_file = open(filename)
xmlcontents = input_file.read()
input_file.close()
xmlcontents = xmlcontents.replace("spam", "eggs")
output_file = open(filename,"w")
output_file.write(xmlcontents)
output_file.close()
Although this works alone it is nto
working when i handle multiple file I/O.Is there a alternative to do
this.(maybe without read() operation)
Thanks

After reading your post again, this might be better:

#!/usr/bin/env python

from elementtree import ElementTree as et
tree = et.parse("testxml.xml")

for t in tree.getiterator("SERVICEPARAMETER"):
if t.get("Semantics") == "localId":
t.set("Semantics", "dataPackageID")

tree.write("output.xml")

~Sean- Hide quoted text -

- Show quoted text -
which module should be imported for above to work,it says
ImportError: No module named elementtree
Thanks

May 10 '07 #6

P: n/a
On May 10, 4:21 am, saif.shak...@gmail.com wrote:
On May 10, 1:55 pm, half.ital...@gmail.com wrote:
On May 10, 12:56 am, saif.shak...@gmail.com wrote:
Hi,
I need to replace a string in xml file with something else.Ex
- <SERVICEPARAMETER id="_775" Semantics="subfunction" DDORef="_54">
<SHORTNAME>rate</SHORTNAME>
<LONGNAME>rate</LONGNAME>
<VALUE role="constant" DataType="unsigned" value="1" />
<BYTEPOSITION role="position" BytePos="1" />
</SERVICEPARAMETER>
- <SERVICEPARAMETER id="_776" Semantics="localId" DDORef="_54">
Here i have opened an xml
file(small part is pasted here).I want to replace the word 'localId'
with 'dataPackageID' wherever it comes in xml file.I have asked this
before and got a code:
input_file = open(filename)
xmlcontents = input_file.read()
input_file.close()
xmlcontents = xmlcontents.replace("spam", "eggs")
output_file = open(filename,"w")
output_file.write(xmlcontents)
output_file.close()
Although this works alone it is nto
working when i handle multiple file I/O.Is there a alternative to do
this.(maybe without read() operation)
Thanks
After reading your post again, this might be better:
#!/usr/bin/env python
from elementtree import ElementTree as et
tree = et.parse("testxml.xml")
for t in tree.getiterator("SERVICEPARAMETER"):
if t.get("Semantics") == "localId":
t.set("Semantics", "dataPackageID")
tree.write("output.xml")
~Sean- Hide quoted text -
- Show quoted text -

which module should be imported for above to work,it says
ImportError: No module named elementtree
Thanks
You can either 1) upgrade to python 2.5 which includes the elementtree
module or 2) download and add the module to your current installation

http://effbot.org/zone/element-index.htm

~Sean

May 10 '07 #7

P: n/a
sa**********@gmail.com wrote:
On May 10, 1:55 pm, half.ital...@gmail.com wrote:
>from elementtree import ElementTree as et

which module should be imported for above to work,it says
ImportError: No module named elementtree
Thanks
What about trying a web search engine to answer your own question? Usually
much faster for this kind of request.

Stefan
May 10 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.