Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the Difference between cElementtree and ElementTree?

Tags:

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.

like image 456
Hai Vu Avatar asked Feb 28 '10 16:02

Hai Vu


People also ask

What is the difference between celementtree and elementtree in Python?

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.

What is the difference between Etree and elementtree?

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.

What is the elementtree module in XML?

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.

What is the difference between lxml Etree and lxml elementtree?

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:


2 Answers

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).

like image 121
Desintegr Avatar answered Oct 21 '22 12:10

Desintegr


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

like image 44
logan Avatar answered Oct 21 '22 12:10

logan