[BangPypers] Parsing and updating XML files with python

Balachandran Sivakumar benignbala at gmail.com
Thu Nov 17 12:17:06 CET 2011


Hi,

             I have to parse an xml file, get to a spefic node, and
set it to a different value and write it back to the file. What is the
best way to do it ? The xml file can be a few hundred lines(with
proper formatting).

For parsing, I use lxml with xpath. I am doing something like this.

from lxml import etree

<---Open the XML file etc.--->
p_data = etree.parse(xml_data)

I use xpath like
p.xpath(/foo/bar[@id=xyz])

But I don't really know how to update an entry and write it back to
the xml file ? Can someone suggest some ways please ? Also, it is
necessary that it should be XPath based. Thanks

-- 
Thank you
Balachandran Sivakumar

Arise Awake and stop not till the goal is reached.
                                                             - Swami Vivekananda

Mail: benignbala at gmail.com
Blog: http://benignbala.wordpress.com/


More information about the BangPypers mailing list