bogumbiker
bogumbiker

Reputation: 527

changing attribute value in xml via lxml python

here is my xml:

<request><table attributeA="50" attributeB="1"></table>........</request>

how do I update attributeA's value, to have something like attributeA="456"

<request><table attributeA="456" attributeB="1"></table>........</request>

Upvotes: 1

Views: 1886

Answers (1)

Thomas Lehoux
Thomas Lehoux

Reputation: 1196

Use etree and xpath :

>>> from lxml import etree
>>> xml = '<request><table attributeA="50" attributeB="1"></table></request>'
>>> root = etree.fromstring(xml)
>>> for el in root.xpath("//table[@attributeA]"):
...     el.attrib['attributeA'] = "456"
...
>>> print etree.tostring(root)
<request><table attributeA="456" attributeB="1"/></request>

Upvotes: 2

Related Questions