Help regarding XML file manipulation

Shashank Singh shashank.sunny.singh at gmail.com
Tue May 25 05:29:12 EDT 2010


On Tue, May 25, 2010 at 2:24 PM, Salil GK <salilgk at gmail.com> wrote:

> Hello
>
>   I need to make a quick prototype to process some xml file. I found
> xml.dom.minidom much handy and I can get information from the xml file. But
> I need to update a filed in the xml file and save the file. I couldn't find
> a way to do that. Could some one please help me on this.
>
> Basically this is what I want to do
>
> <?xml version="1.0" encoding="UTF-8"?>
> <TopLevel>
>  <Level2>
>   <Engineer>Hari</Engineer>
>   <DOJ>102010</DOJ>
>   <NextLeave>201010</NextLeave>
>
>   <Engineer>Rama</Engineer>
>   <DOJ>102010</DOJ>
>   <NextLeave>201010</NextLeave>
>
>  </Level2>
> </TopLevel>
>
> Here I want to read the Engineer information and then change the NextLeave
> information to a new value and save to the same file. How do we do this -
> can some one please help me with some sample code for the same.
>

have a look at the xml.dom.minidom module.
You can access nodes by getElementsByTagName("Engineer")
Access value of the text node by node.nodeValue

To modify the xml, you will have to set the node value to the desired value
node.nodeValue = newValue

Note that to you will have to write the modified dom object back to the file
to save the changes, just setting the value of nodes will not automatically
do that.

HTH
-S

>
> Thanks
> ~S
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


-- 
Regards
Shashank Singh
Senior Undergraduate, Department of Computer Science and Engineering
Indian Institute of Technology Bombay
shashank.sunny.singh at gmail.com
http://www.cse.iitb.ac.in/~shashanksingh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100525/9e7f4e56/attachment.html>


More information about the Python-list mailing list