0
<?xml version="1.0"?>
<info>
  </tags>
  </tags>
  <area>
<media>
    <options>
         <name>Jaipur</name>
    </options>
</media>
  </area>
</info>


i am totaly new in python, here is my xml file and i want to edit element value at run time in python
it means I want to change the <name>Jaipur</name> to <name>Mumbai</name>

madan
  • 773
  • 13
  • 38

1 Answers1

1

First, the example is not valid xml. You can use xml.etree that comes included:

from xml.etree import ElementTree as et
xmlstr="""\
<?xml version="1.0"?>
<area>
  <media>
    <options>
         <name>Jaipur</name>
    </options>
 </media>
</area>"""
doc=et.fromstring(xmlstr)
doc.find('.//name').text='Mumbai'
print et.tostring(doc)

output:

<area>
  <media>
    <options>
         <name>Mumbai</name>
    </options>
 </media>
</area>
root
  • 76,608
  • 25
  • 108
  • 120