Python and XML help

Clark C . Evans cce at clarkevans.com
Sat Jul 27 13:06:19 EDT 2002


On Fri, Jul 26, 2002 at 10:03:43AM -0700, Mathieu wrote:
| <properties name="spam">
| <prop>
| <first>
|  <item name="foo">
|  <item name="foo2">
| </first>
| 
| <second>
|  <item name="bar">
|  <item name="bar2">
| </second>
| </prop>

For a simple properties list like this you could use YAML
instead of python... it'd be alot easier.  This way you
don't have to mess with DOM or SAX.

$ wget http://yaml.org/python/PyYaml_25jul2002.tgz
$ tar xvfz PyYaml_25jul2002.tgz
$ cd PyYaml
$ python setup.py install

$ python
ActivePython 2.2.1 Build 222 (ActiveState Corp.) based on
Python 2.2.1 (#34, Apr 15 2002, 09:51:39) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import yaml
>>> x = yaml.load("""
... properties: spam
... prop:
...   first:
...     - foo
...     - foo2
...   second:
...     - bar
...     - bar2
... """).next()

>>> print x  # with slight formatting for readability...
{'properties': 'spam', 
       'prop': {'second': ['bar', 'bar2'], 
                'first': ['foo', 'foo2']}}

>>> x["prop"]["first"][0]
'foo'

>>> print yaml.dump(x)
---
prop:
    first:
        - foo
        - foo2
    second:
        - bar
        - bar2
properties: spam

>>> ^Z


Best,

Clark




More information about the Python-list mailing list