Update an attribute in xml file using python

This is the place for queries that don't fit in any of the other categories.

Update an attribute in xml file using python

Postby Sasikiran » Fri Feb 22, 2013 11:32 am

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'/>
<source file='/PART2/kvm3/images/ubuntu_11-10_template-1-clone-4-clone-clone-59.img'/>
<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
Sasikiran
 
Posts: 6
Joined: Fri Feb 22, 2013 11:16 am

Re: Update an attribute in xml file using python

Postby stranac » Fri Feb 22, 2013 12:08 pm

First, make sure you have valid xml.
The example you've shown is missing the closing </devices> tag.

I would recommend using lxml for this.
Example, with the corrected xml as string in variable xml_string:
Code: Select all
import lxml.etree

doc = lxml.etree.fromstring(xml_string)

# select all source elements that have a file attribute, and take the first one
source = doc.xpath('//source[@file]')[0]
# change the file attribute
source.attrib['file'] = 'hello.txt'

new_xml = lxml.etree.tostring(doc)
print new_xml

Output:
Code: Select all
<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"/>
<source file="hello.txt"/>
<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>
</devices>
</root>
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1111
Joined: Thu Feb 07, 2013 3:42 pm

Re: Update an attribute in xml file using python

Postby Sasikiran » Fri Feb 22, 2013 12:49 pm

I missed that ,

If the xml data is reading from a file. Then how can we read it.

As i am facing some issues while reading it.

Thanks in advance
Sasikiran
Sasikiran
 
Posts: 6
Joined: Fri Feb 22, 2013 11:16 am

Re: Update an attribute in xml file using python

Postby Yoriz » Fri Feb 22, 2013 1:24 pm

There is a tutorial on files here if that doesn't solve your issues you will need to be more specific about what the issues are by showing your full error traceback and the code you tried.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Update an attribute in xml file using python

Postby Sasikiran » Fri Feb 22, 2013 1:57 pm

Thanks for the help,

Initially while taking the xmldata from a file, i made a small mistake in storing the data after parsing from file.
Now there is no problem
Sasikiran
 
Posts: 6
Joined: Fri Feb 22, 2013 11:16 am

Re: Update an attribute in xml file using python

Postby snippsat » Fri Feb 22, 2013 4:24 pm

Alternative with BeautifulSoup.
BeautifulSoup and lxml are both very good,and the only parser i would use.
Both can handle unvalid/garbage xml/html,here do other parser fail.
This is more important when parsing html,where garbage factor is normal ;)
Code: Select all
from bs4 import BeautifulSoup

xml = '''\
<source file='/PART2/kvm3/images/ubuntu_11-10_template-1-clone-4-clone-clone-59.img'/>'''

soup = BeautifulSoup(xml, 'xml')
tag = soup.find('source')
tag['file'] = 'hello.txt'
print tag #<source file="hello.txt"/>
Last edited by snippsat on Fri Feb 22, 2013 5:14 pm, edited 1 time in total.
User avatar
snippsat
 
Posts: 163
Joined: Thu Feb 21, 2013 12:04 am

Re: Update an attribute in xml file using python

Postby stranac » Fri Feb 22, 2013 5:07 pm

Sasikiran wrote:If the xml data is reading from a file. Then how can we read it.

You could use normal python file handling, but if you're using lxml, there is also a parse() function, which can create a document out of a few different things:
The parse() function supports any of the following sources:

  • an open file object (make sure to open it in binary mode)
  • a file-like object that has a .read(byte_count) method returning a byte string on each call
  • a filename string
  • an HTTP or FTP URL string
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1111
Joined: Thu Feb 07, 2013 3:42 pm

Re: Update an attribute in xml file using python

Postby Sasikiran » Sat Feb 23, 2013 5:40 am

If we want to change the text of the following tag then,

Reading the "xml data" from a file , performing updations and creating a new file with the updated changes.

<emulator>/usr/bin/kvm</emulator>

to

<emulator>qemu</emulator>


Atlast the entire data with modifications should save in other file suppose test.xml

I am new to python, so i was confused with the parsing, updations and again saving it.

Please help me on this
Sasikiran
 
Posts: 6
Joined: Fri Feb 22, 2013 11:16 am

Re: Update an attribute in xml file using python

Postby stranac » Sat Feb 23, 2013 12:10 pm

Here's an lxml example that does all of that.
I would also recommend carefully reading the documentation and tutorials at http://lxml.de/
Code: Select all
import lxml.etree

# create xml document from a file
doc = lxml.etree.parse('old.xml')

# select your element and do the necessary changes
emulator_element = doc.xpath('//emulator')[0]
emulator_element.text = 'qemu'

# write the document to a new file
doc.write('new.xml')
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1111
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], Yoriz and 2 guests