update an xml attribute using python

2,802 views
Skip to first unread message

sasikiran v

unread,
Feb 22, 2013, 6:40:08 AM2/22/13
to pytho...@googlegroups.com
Hi,

I want to update an xml attribute

For Eg.

<root>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' unit='0'/>
    </disk>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
   [b]   <source file='/PART2/kvm3/images/ubuntu_11-10_template-1-clone-4-clone-clone-59.img'/>[/b]
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' unit='0'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:2e:00:75'/>
      <source bridge='br100'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
</disk>
</root>

This is the sample xml file

Can you please help in implementing a python script which updates the file ='hello.txt" (bolded line)

Note: There are two source tags but i want the source tag which contain file as a attribute to be updated with file='hello.txt'

Thanks in Advance

Sasikiran

Dhananjay Nene

unread,
Feb 22, 2013, 6:59:10 AM2/22/13
to PythonPune
Can you write what you've attempted so far ? If there are any specific issues with the implementation we can attempt to help out with it. 

alternatively search for some minidom tutorials. Perhaps use http://www.travisglines.com/web-coding/python-xml-parser-tutorial to get started, also check up the minidom documentation and attempt to change a value. Post a response back if it does not work out.

sasikiran v

unread,
Feb 22, 2013, 7:31:58 AM2/22/13
to pytho...@googlegroups.com
i tried with some alternatives but it is checking for one attribute value and based on that value it is getting replaced.

For Eg.

#!/usr/bin/python

from elementtree.ElementTree import XML, SubElement, Element, tostring

text = """
<root>
    <phoneNumbers>
        <number topic="sys/phoneNumber/1" update="none" />
        <number topic="sys/phoneNumber/2" update="none" />
        <number topic="sys/phoneNumber/3" update="none" />
    </phoneNumbers>

    <gfenSMSnumbers>
        <number topic="sys2/SMSnumber/1" update="none" />
        <number topic="sys2/SMSnumber/2" update="none" />
    </gfenSMSnumbers>
</root>
"""
print "********************"
print text
print "*******************"

elem = XML(text)
for node in elem.find('phoneNumbers'):
    print node.attrib['topic']
    # Create sub elements
    if node.attrib['topic']=="sys/phoneNumber/1":
        tag = SubElement(node,'TagName')
        tag.attrib['attr'] = 'AttribValue'

print tostring(elem)


from the above one it is checking the condition
if node.attrib['topic']=="sys/phoneNumber/1":

after that it is adding something to it, but i want if any tag contains attribute "topic" it should replace the value with some text like "hello.txt"

Dhananjay Nene

unread,
Feb 22, 2013, 8:29:42 AM2/22/13
to PythonPune
On Fri, Feb 22, 2013 at 6:01 PM, sasikiran v <sasiki...@gmail.com> wrote:
i tried with some alternatives but it is checking for one attribute value and based on that value it is getting replaced.

For Eg.

Here's what the documentation tells us about attrib (in http://docs.python.org/2/library/xml.etree.elementtree.html)

"attrib
A dictionary containing the element’s attributes. Note that while the attrib value is always a real mutable Python dictionary, an ElementTree implementation may choose to use another internal representation, and create the dictionary only if someone asks for it. To take advantage of such implementations, use the dictionary methods below whenever possible."


after that it is adding something to it, but i want if any tag contains attribute "topic" it should 
replace the value with some text like "hello.txt"

As you can see the attrib dictionary returned to you is a real mutable python dictionary but it is not the real internal representation. So modifying that dict will have no effect. Instead what the API refers us to is 

set(keyvalue)

Set the attribute key on the element to value.

Mystery solved.

from elementtree.ElementTree import XML, SubElement, Element, tostring

text = """
<root>
    <phoneNumbers>
        <number topic="sys/phoneNumber/1" update="none" />
        <number topic="sys/phoneNumber/2" update="none" />
        <number topic="sys/phoneNumber/3" update="none" />
    </phoneNumbers>

    <gfenSMSnumbers>
        <number topic="sys2/SMSnumber/1" update="none" />
        <number topic="sys2/SMSnumber/2" update="none" />
    </gfenSMSnumbers>
</root>
"""
#print "********************"
#print text
#print "*******************"

elem = XML(text)
for node in elem.find('phoneNumbers'):
    print node.attrib['topic']
    # Create sub elements
    if node.attrib['topic']=="sys/phoneNumber/1":
        node.set('topic',"foobar")

print tostring(elem)
--
You received this message because you are subscribed to the Google Groups "Python Pune" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonpune+...@googlegroups.com.
To post to this group, send email to pytho...@googlegroups.com.
Visit this group at http://groups.google.com/group/pythonpune?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
----------------------------------------------------------------------------------------------------------------------------------
http://blog.dhananjaynene.com twitter: @dnene google plus: http://gplus.to/dhananjaynene
Reply all
Reply to author
Forward
0 new messages