Parsing XML: SAX, DOM, Expat, or Something Else?

Discussion in 'Python' started by aha, Jan 23, 2009.

  1. aha

    aha Guest

    Hello All,
    I've been charged with developing an XML configuration file format,
    for one of the applications that my company develops. (Yes, I know it
    would have been easier to just use the configuration file format as
    described in RFC 822) While I am finally comfortable with the XML
    description that we've agreed upon; I am still trying to determine the
    best XML parser API to use. I would love to use XPATH, however I face
    the following restriction:

    The Python scripts that perform the parsing of the XML configuration
    file must be compatible with Python Versions 2.2-present.

    This means that any functionality that I use needs to be compatible
    with Python 2.2.

    I started with the DOM API in xml.dom.minidom and I thought that it
    might be the best way to go, but then I ran across the Recipes of Wai
    Yip Tung

    http://code.activestate.com/recipes/534109/


    and John Bair, Christoph Dietze from the second edition of the Python
    cookbook.

    Wai's implementation uses thes SAX parser and John and Christoph's
    implementation uses Expat API.

    In the end what I really want is to transform the XML

    <config>
    <component>
    <setting></setting>
    </component>
    </config>

    into an object that looks like

    config.component.setting or a map config[component][setting].

    Another restriction is that I don't want to have to ship additional
    modules.

    Does anyone have any advice, comments, or HELP???

    Aquil H. Abdullah
    Developer
     
    aha, Jan 23, 2009
    #1
    1. Advertising

  2. aha

    Chris Rebert Guest

    On Fri, Jan 23, 2009 at 11:19 AM, aha <> wrote:
    > Hello All,
    > I've been charged with developing an XML configuration file format,
    > for one of the applications that my company develops. (Yes, I know it
    > would have been easier to just use the configuration file format as
    > described in RFC 822) While I am finally comfortable with the XML
    > description that we've agreed upon; I am still trying to determine the
    > best XML parser API to use. I would love to use XPATH, however I face
    > the following restriction:
    >
    > The Python scripts that perform the parsing of the XML configuration
    > file must be compatible with Python Versions 2.2-present.
    >
    > This means that any functionality that I use needs to be compatible
    > with Python 2.2.
    >
    > I started with the DOM API in xml.dom.minidom and I thought that it
    > might be the best way to go, but then I ran across the Recipes of Wai
    > Yip Tung
    >
    > http://code.activestate.com/recipes/534109/
    >
    >
    > and John Bair, Christoph Dietze from the second edition of the Python
    > cookbook.
    >
    > Wai's implementation uses thes SAX parser and John and Christoph's
    > implementation uses Expat API.
    >
    > In the end what I really want is to transform the XML
    >
    > <config>
    > <component>
    > <setting></setting>
    > </component>
    > </config>
    >
    > into an object that looks like
    >
    > config.component.setting or a map config[component][setting].
    >
    > Another restriction is that I don't want to have to ship additional
    > modules.
    >
    > Does anyone have any advice, comments, or HELP???


    ElementTree is in the std lib for Python 2.5+ and has a fairly
    Pythonic API compared to SAX/DOM.
    Docs for it: http://docs.python.org/library/xml.etree.elementtree.html

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
     
    Chris Rebert, Jan 23, 2009
    #2
    1. Advertising

  3. Chris Rebert wrote:
    > On Fri, Jan 23, 2009 at 11:19 AM, aha <> wrote:
    >> I've been charged with developing an XML configuration file format,
    >> for one of the applications that my company develops.
    >> [...]
    >> I am still trying to determine the
    >> best XML parser API to use. I would love to use XPATH, however I face
    >> the following restriction:
    >>
    >> The Python scripts that perform the parsing of the XML configuration
    >> file must be compatible with Python Versions 2.2-present.

    >
    > ElementTree is in the std lib for Python 2.5+ and has a fairly
    > Pythonic API compared to SAX/DOM.
    > Docs for it: http://docs.python.org/library/xml.etree.elementtree.html


    The downloadable package (version 1.2.x) still works with Python 2.2 (last
    I heard, at least). ET also has simple support for XPath-like expressions
    (without conditions, that is). Starting to use it now will however allow
    you to switch to the mostly compatible lxml.etree package once you decide
    to move on to Python 2.3 or later. lxml has full support for XPath (and
    XSLT and tons of other goodies).

    Stefan
     
    Stefan Behnel, Jan 23, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Guettler

    xml.parsers.expat vs. xml.sax

    Thomas Guettler, Apr 27, 2004, in forum: Python
    Replies:
    2
    Views:
    933
    Martijn Faassen
    Apr 27, 2004
  2. sharan
    Replies:
    1
    Views:
    739
    Pavel Lepin
    Oct 26, 2007
  3. Replies:
    2
    Views:
    971
    Joseph Kesselman
    Nov 5, 2007
  4. kj
    Replies:
    2
    Views:
    297
  5. Erik Wasser
    Replies:
    5
    Views:
    531
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page