35
from lxml import objectify, etree

root = etree.fromstring('''<?xml version="1.0" encoding="ISO-8859-1" ?>
<scenario>
<init>
    <send channel="channel-Gy">
        <command name="CER">
            <avp name="Origin-Host" value="router1dev"></avp>
            <avp name="Origin-Realm" value="realm.dev"></avp>
            <avp name="Host-IP-Address" value="0x00010a248921"></avp>
            <avp name="Vendor-Id" value="11"></avp>
            <avp name="Product-Name" value="HP Ro Interface"></avp>
            <avp name="Origin-State-Id" value="1094807040"></avp>
            <avp name="Supported-Vendor-Id" value="10415"></avp>
            <avp name="Auth-Application-Id" value="4"></avp>
            <avp name="Acct-Application-Id" value="0"></avp>
            <avp name="Vendor-Specific-Application-Id">
                <avp name="Vendor-Id" value="11"></avp>
                <avp name="Auth-Application-Id" value="4"></avp>
                <avp name="Acct-Application-Id" value="0"></avp>
            </avp>
            <avp name="Firmware-Revision" value="1"> </avp>
        </command>
    </send>
</init>

<traffic>
    <send channel="channel-Gy" >
        <action>
            <inc-counter name="HbH-counter"></inc-counter>
            ....
        </action>
    </send>
</traffic>
</scenario>''')

How can I modify/set both values?

  • Host-IP-Address value="0x00010a248921"

  • "Vendor-Id" value="11"

I've unsuccessfully tried accessing

root.xpath("//scenario/init/send_channel/command[@name='CER']/avp[@name='Host-IP-Address']/value/text()")

Goal: I'd preferably like to see a lxml.objectify vs an Xpath solution but I'll accept other lxml based solutions.

The files are <100kB so speed/RAM is not much of a concern.

Joao Figueiredo
  • 3,120
  • 3
  • 31
  • 40

2 Answers2

45
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)
Acorn
  • 49,061
  • 27
  • 133
  • 172
  • Thanks Acorn. I can access the attributes flawlessly this way, but how can I set this new host_ip value and write the resulting Element back into an .xml file? – Joao Figueiredo Nov 17 '11 at 18:33
  • I'm changing the attribute in the snippet above, just use normal dictionary notation. After you've changed the attributes you want to change, you can just do `et.tostring(tree)`. – Acorn Nov 17 '11 at 18:35
  • Acorn, I'm afraid I didn't follow you. My initial question was really about changing those attributes, not only finding them. I've set the host_ip.set('value', 'foobar') but what's missing so the changes get propagated so I can with open("out.xml", "w") as ofile: ofile.write(et.tostring(tree)) ? – Joao Figueiredo Nov 17 '11 at 19:10
  • There's nothing else to do, by changing the attributes you're changing the tree. – Acorn Nov 17 '11 at 19:32
  • It is changing the tree. I was probably changing it erroneously to another Element instance and wasn't seeing the changes. Thank you. Answer approved. – Joao Figueiredo Nov 18 '11 at 09:29
  • 1
    @Acorn .attrib does not work with attributes such as 'draw:image:'. Any idea how to add such attributes? – Shreedhar Manek Jun 09 '16 at 00:25
8

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".

larsks
  • 277,717
  • 41
  • 399
  • 399
  • r.find('//avp[@name="Host-IP-Address"]') returns SyntaxError: cannot use absolute path on element. The correct method should be attrib() then right? – Joao Figueiredo Nov 17 '11 at 17:57
  • 2
    It turns out that etree.fromstring() actually returns something other than etree.parse(), which was unexpected. You can turn the path into a relative path by using `.//avp[@name="Host-IP-Address"]`, which will work just fine. "." represents "the current node". – larsks Nov 17 '11 at 21:55