xml2dict - dict2xml

Henning Peters pete at dexterslab.de
Tue Apr 16 06:31:52 EDT 2002


hi NG,

i am searching for the best solution to convert a small xml fragment in a
python dictionary and vice versa.
speed is a very important factor, so i have tried the xml.parsers.expat
interface, but i did not found out, how to map a tree with it (i.e. to parse
only one branch).

this...

<cmd name="foo">
  <var>var1</var>
  <var>var2</var>
  <var type="hash">
    <var key="a">var1</var>
    <var key="b">var2</var>
  </var>
</cmd>

...should be converted to a dict like this:
{'cmd': 'foo', 'var':('var1', 'var2', {'a':'var1', 'b':'var2'})}

in most cases the xml data is very small (<<1kByte).
it is only necessary to be able to map strings (var), hashes (var
type="hash") and lists (var type="list")
all vars are encapsulated by a cmd tag. the cmd tag is the root tag
the way back should work similar...

what would you suggest is the best (and fast) way to do these conversions?
do you know whether there already exist such an approach?

bye,
henning






More information about the Python-list mailing list