reading xml from python

Chris Rebert clp2 at rebertia.com
Tue May 11 09:17:07 EDT 2010


On Tue, May 11, 2010 at 5:54 AM, Hvidberg, Martin <mhv at dmu.dk> wrote:
> I'm looking for at way to read (and later write) small simple .xml file from
> Python.
>
> e.g. I would like to read the following from a small ini.xml file into a
> dictionary.
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <initialisation>
>  <idrectory>default</idrectory>
>  <nosplit>False</nosplit>
>  <nobatch>False</nobatch>
>  <ubmmode>UBMlight</ubmmode>
>  <overwrite_output>True</overwrite_output>
> </initialisation>
>
> I would prefer a relative simple (not too much creating new classes) way to
> do this.

from xml.etree.ElementTree import ElementTree
tree = ElementTree()
tree.parse("ini.xml")
as_dictionary = dict((child.tag, child.text) for child in tree.getchildren())

Writing is left as an exercise for another poster.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list