replacing string in xml file--revisited

S

saif.shakeel

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
 
M

Marc 'BlackJack' Rintsch

saif.shakeel said:
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
 
H

half.italian

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
 
H

half.italian

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
 
S

saif.shakeel

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
 
S

saif.shakeel

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
 
S

Stefan Behnel

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

replacing string in xml file 6
i/o prob revisited 3
Removing part of string 4
Problems in string replacement 0
f python? 19

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top