replacing string in xml file

saif.shakeel at gmail.com saif.shakeel at gmail.com
Thu May 10 00:55:25 EDT 2007


On May 9, 4:39 pm, saif.shak... at gmail.com wrote:
> On May 8, 4:46 pm, saif.shak... at gmail.com wrote:
>
>
>
>
>
> > On May 8, 4:30 pm, Stefan Behnel <stefan.behnel-n05... at web.de> wrote:
>
> > > saif.shak... at gmail.com schrieb:
>
> > > > 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 tried this but
> > > > didnt work:
>
> > > > import sys
>
> > > > file_input = raw_input("Enter The ODX File Path:")
> > > > input_xml = open(file_input,'r')
>
> > > This should say
>
> > >   input_xml = open(file_input,'r').read()
>
> > > > input_xml.replace('localId','dataPackageId')
> > > >                      This gives error  ---> AttributeError: 'file'
> > > > object has no attribute 'replace'
> > > >         Can someone help me .
> > > >                   Thanks
>
> > > Stefan- Hide quoted text -
>
> > > - Show quoted text -
>
> > There is no error now,but the string is not being replaced,It remains
> > the same,should we save the opened file or something- Hide quoted text -
>
> > - Show quoted text -
>
> HI,
>          Thanks for the reply.that seems to work,but i was doing this
> so as to attach it to a bigger code where it will be utilised before a
> parsing.
>
> #Input file and Output file path from user
>
> file_input = raw_input("Enter The ODX File Path:")
>
> (shortname,ext)=os.path.splitext(file_input)
> f_open_out=shortname+".ini"
> log=shortname+".xls"
> test_file=shortname+"testxml.xml"
>
> saveout = sys.stdout
>
> input_xml = open(file_input,'r')
> xmlcont=input_xml.read()
> xmlcont=xmlcont.replace('localId','dataPackageId')
> output_file = open(test_file,"w")
> output_file.write(xmlcont)
> output_file.close()
>
> f_open=open(f_open_out, 'w')
> logfile=open(log,"w")
>
> sys.stdout = f_open
>
> #Parse the input file,and check for correct ODX  version
>
> xmldoc = minidom.parse(input_xml)
>
>                     I am opening 2 more files in addition to the file
> where the new xml goes.One file is written using the sys.stdout
> command as most of the output has to go there printing takes place in
> many places (so cant use f_open.write) each time.
>                     When i attach the part of replacing the string
> 'localid' in xml file with something else as given above with
> xmlcont=xmlcont.replace('localId','dataPackageId')
> the code does not run and hangs.Can more than 3 files be opened at a
> time .I dotn know what the problem is here.
>                      Thanks- Hide quoted text -
>
> - Show quoted text -

Hi,
    Cna someone help me in this ,or throw some insight .
                                   Thanks




More information about the Python-list mailing list