[BangPypers] Parsing and updating XML files with python

Gora Mohanty gora at mimirtech.com
Thu Nov 17 14:57:34 CET 2011


On Thu, Nov 17, 2011 at 4:47 PM, Balachandran Sivakumar
<benignbala at gmail.com> wrote:
> 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).
[...]
> 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
[...]

To update an element, just assign a value to it, e.g.,
  myelem.text = "New value"
You can then save the modified XML to a new file.
Please see http://infohost.nmt.edu/tcc/help/pubs/pylxml/web/modifying.html

That same tutorial,
http://infohost.nmt.edu/tcc/help/pubs/pylxml/web/index.html ,
 also describes various ways of addressing elements. This is also a useful
tutorial: http://lxml.de/tutorial.html

Regards,
Gora


More information about the BangPypers mailing list