python lxml - modify attributes

爷,独闯天下 提交于 2019-11-29 05:33:40
import lxml.etree as et

tree = et.fromstring('''
... your xml ...
''')

for host_ip in tree.xpath("/scenario/init/send/command[@name='CER']/avp[@name='Host-IP-Address']"):
    host_ip.attrib['value'] = 'foo'

print et.tostring(tree)

You could try this:

r = etree.fromstring('...')

element = r.find('//avp[@name="Host-IP-Address"]')

# Access value
print 'Current value is:', element.get('value')

# change value
element.set('value', 'newvalue')

Also, note that in your example you're using the text() method, but that's not what you want: the "text" of an element is what is enclosed by the element. For example, given this:

<someelement>this is the text</someelement>

The value of the text() method on the <somevalue> element is "this is the text".

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!