I am using python xmlElementTree and want to assign or modify a xml element value based on its attribute. Can somebody give me an idea how to do this?
For example: Here
I'm not familiar with xmlElementTree
, but if you're using something capable of xpath
expressions you can locate a node by attribute value using an expression like this:
//number[@topic="sys/phoneNumber/1"]
So, using the etree
module:
>>> import lxml.etree as etree
>>> doc = etree.parse('foo.xml')
>>> nodes = doc.xpath('//number[@topic="sys/phoneNumber/1"]')
>>> nodes
[]
>>> etree.tostring(nodes[0])
' \n '