XML::Simple -- Python equiv?

Gisle Aas gisle at ActiveState.com
Fri Aug 11 08:40:22 EDT 2000


garethn at solvera-inform.com (Gareth Noyce) writes:

> I'd like to open an XML document and have the tags and thier contents
> accessible as objects in Python. Perl has the XML::simple module which
> works wonders. What's the best solution for Python? 

Well,... I would just do something like this:

$ python
Python 1.5.2 (#3, Jun  5 2000, 20:59:58)  [GCC egcs-2.91.66 19990314/Linux (egcs- on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import perl
>>> perl.require("XML::Simple")
1
>>> XMLin = perl.get_ref("XML::Simple::XMLin")
>>> config = XMLin("/home/gisle/foo.xml")
>>> config["logdir"]
'/var/log/foo/'
>>> config["server"].keys()
['sahara', 'gobi', 'kalahari']
>>> config["server"]["gobi"]["address"]
'10.0.0.102'
>>> XMLin
<perl CODE(0x8148aa4) ref at 80f42a8>
>>> config
<perl HASH(0x824627c) ref at 8247e78>

:-)

-- 
Gisle Aas



More information about the Python-list mailing list