xml2dict - dict2xml

David Mertz, Ph.D. mertz at gnosis.cx
Tue Apr 16 12:19:19 EDT 2002


|Henning Peters wrote:
|> i am searching for the best solution to convert a small xml fragment in a
|> python dictionary and vice versa.

Thomas Guettler <zopestoller at thomas-guettler.de> wrote previously:
|http://www-106.ibm.com/developerworks/xml/library/x-matters11.html

A somewhat more up to date source than my abovementioned article is:

    http://gnosis.cx/download/Gnosis_XML_Util.ANNOUNCE

Our xml_pickle utility converts (very nearly) any Python object to XML,
and can restore it back to a Python object (including a dictionary).
What xml_pickle handles should be a strict superset of what pickle.py
handles, since at very worst, we can fall back to inserting a pickle
fragment inside an element (which isn't very XML-ish, but also doesn't
happen except in very strange cases).  The XML format is not quite the
same as the prior example, however.  It wasn't clear to me whether the
main issue was the Python dictionary or the specific XML format.  What
you get looks like:

    >>> from gnosis.xml.pickle import dumps
    >>> dct = {1:2, 'this':'that'}
    >>> print dumps(dct)
    <?xml version="1.0"?>
    <!DOCTYPE PyObject SYSTEM "PyObjects.dtd">
    <PyObject family="obj" type="builtin_wrapper"  class="_EmptyClass">
    <attr name="__toplevel__" type="dict" id="799116" >
      <entry>
        <key type="string" value="this" />
        <val type="string" value="that" />
      </entry>
      <entry>
        <key type="numeric" value="1" />
        <val type="numeric" value="2" />
      </entry>
    </attr>
    </PyObject>

Usage is simple.  If you need specific XML tags and structures, however,
you'll probably need to write your own code (or customize ours).

xml_pickle now optionally uses SAX instead of DOM for underlying
parsing, which is pretty fast.  Henning Peters' simultaneous statements
that speed was essential, and that small dictionaries were at issue,
seems slightly strange.  Certainly for any "small" dictionary,
xml_pickle is pretty much too fast to accurately time.  But the point of
xml_pickle is not speed... a custom application, probably especially
using PyRXP, would be faster (but might not handle new cases one
discovered with usage).  I suspect the speed issue is actually a red
herring though, and most any approach will be "fast enough".  FWIW:  if
you want to transmit or store an xml_pickle file, we have a compression
option, which produces output that is smaller than binary pickles!

Yours, David...

--
mertz@  | The specter of free information is haunting the `Net!  All the
gnosis  | powers of IP- and crypto-tyranny have entered into an unholy
.cx     | alliance...ideas have nothing to lose but their chains.  Unite
        | against "intellectual property" and anti-privacy regimes!
-------------------------------------------------------------------------






More information about the Python-list mailing list