I know a little of dom, and would like to learn about ElementTree. Python 2.6 has a somewhat older implementation of ElementTree, but still usable. However, it looks like it comes with two different classes: xml.etree.ElementTree and xml.etree.cElementTree. Would someone please be so kind to enlighten me with their differences? Thank you.
It is the same library (same API, same features) but ElementTree is implemented in Python and cElementTree is implemented in C. If you can, use the C implementation because it is optimized for fast parsing and low memory use, and is 15-20 times faster than the Python implementation.
Because etree is built on top of libxml2, which is namespace prefix aware, etree preserves namespaces declarations and prefixes while ElementTree tends to come up with its own prefixes (ns0, ns1, etc). When no namespace prefix is given, however, etree creates ElementTree style prefixes as well.
The xml.etree.ElementTree module implements a simple and efficient API for parsing and creating XML data. Changed in version 3.3: This module will use a fast implementation whenever available. Deprecated since version 3.3: The xml.etree.cElementTree module is deprecated.
Importing etree is obviously different; etree uses a lower case package name, while ElementTree a combination of upper-case and lower case in imports: When switching over code from ElementTree to lxml.etree, and you're using the package name prefix 'ElementTree', you can do the following:
It is the same library (same API, same features) but ElementTree is implemented in Python and cElementTree is implemented in C.
If you can, use the C implementation because it is optimized for fast parsing and low memory use, and is 15-20 times faster than the Python implementation.
Use the Python version if you are in a limited environment (C library loading not allowed).
But now they are the same thing as of Python 3.3, in github source code cElementTree
# cElementTree.py
from xml.etree.ElementTree import *
it is just for backward compatibility
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With