0

from xml.dom import minidom
dict[1] = "country"
xmldoc = minidom.parse('TestStep.xml')
itemlist = xmldoc.getElementsByTagName('TestStepBody')
print len(itemlist)
print itemlist[0].attributes['Author'].value
itemlist[0].setAttribute("Author",dict[1]);
for s in itemlist :
print s.attributes['Author'].value

dict[1] value will be changing dynamically, So i need to dynamically change the attribute value of the Author in python. I this possible? if so Can any one help me in this.

Edited by racigan: missed one line of code

2
Contributors
1
Reply
11
Views
2 Years
Discussion Span
Last Post by chriswelborn
0

I don't really use the xml module, and it would really help if you could format your code. But this is what I found:

s = '<foo name="test"><foobar name="original"></foobar></foo>'
d = minidom.parseString(s)
elems = d.getElementsByTagName('foobar')
elems[0].setAttribute('name', 'changed!')
print(d.toprettyxml(indent='    '))

Output:

<?xml version="1.0" ?>
<foo name="test">
    <foobar name="changed!"/>
</foo>

The element is changed. Instead of print, just write it to a file. Hint:

with open('myfile.new.xml', 'w') as f:
    d.writexml(f, addindent='    ', newl='\n')

Or:

with open('myfile.new.xml', 'w') as f:
    f.write(d.toprettyxml(indent='    '))

Edited by chriswelborn: Better example.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.