473,480 Members | 3,135 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

replacing string in xml file--revisited

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
7 3056
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
686
by: Oliver Spiesshofer | last post by:
Hi, I would like to replace all strings in a table with regexp: the strings contain the substring "-na", and I would like to replace the whole table field with the original content but without...
11
2121
by: lucky | last post by:
hi, i got file which contains "----------------" in a line. the line only contains this data as a saperation. using regular expression i want to i detify the line contains that data and replace...
3
1477
by: John | last post by:
Hi How can I replace all occurrences of a character in a string to another character? Thanks Regards
6
5771
by: saif.shakeel | last post by:
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>...
1
1886
by: karunashok | last post by:
hi, i want to read one html file and replace the string between the start and end tag with some string. i need help please help me.
3
1242
by: sharonrao123 | last post by:
Hello all, I have a products table (see below) Product State Apple |1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27| I have to display the state data on a webform...
1
2315
by: susan10 | last post by:
Im new to Coldfusion, and here is what i would like to do: I'm using a html-editor on one cfm-page, and saving the text in a database: --mytext written i the html-editor ----------------------...
5
2374
by: VigneshMohan | last post by:
Hi All I wanted to replace the String Concatenation using " + " with a string buffer to eliminate performance hogs. I have a string String str = "Testing "+ firstStrVariable + "NextTest" +...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7110
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6763
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4503
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3015
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
574
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
210
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.