| 1 |
Why lxml? |
| 2 |
========= |
| 3 |
|
| 4 |
.. contents:: |
| 5 |
.. |
| 6 |
1 Motto |
| 7 |
2 Aims |
| 8 |
|
| 9 |
|
| 10 |
Motto |
| 11 |
----- |
| 12 |
|
| 13 |
"the thrills without the strangeness" |
| 14 |
|
| 15 |
To explain the motto: |
| 16 |
|
| 17 |
"Programming with libxml2 is like the thrilling embrace of an exotic stranger. |
| 18 |
It seems to have the potential to fulfill your wildest dreams, but there's a |
| 19 |
nagging voice somewhere in your head warning you that you're about to get |
| 20 |
screwed in the worst way." (`a quote by Mark Pilgrim`_) |
| 21 |
|
| 22 |
Mark Pilgrim was describing in particular the experience a Python programmer |
| 23 |
has when dealing with libxml2. The default Python bindings of libxml2 are |
| 24 |
fast, thrilling, powerful, and your code might fail in some horrible way that |
| 25 |
you really shouldn't have to worry about when writing Python code. lxml |
| 26 |
combines the power of libxml2 with the ease of use of Python. |
| 27 |
|
| 28 |
.. _`a quote by Mark Pilgrim`: https://web.archive.org/web/20110902041836/http://diveintomark.org/archives/2004/02/18/libxml2 |
| 29 |
|
| 30 |
|
| 31 |
Aims |
| 32 |
---- |
| 33 |
|
| 34 |
The C libraries libxml2_ and libxslt_ have huge benefits: |
| 35 |
|
| 36 |
* Standards-compliant XML support. |
| 37 |
|
| 38 |
* Support for (broken) HTML. |
| 39 |
|
| 40 |
* Full-featured. |
| 41 |
|
| 42 |
* Actively maintained by XML experts. |
| 43 |
|
| 44 |
* fast. fast! FAST! |
| 45 |
|
| 46 |
.. _libxml2: http://www.xmlsoft.org |
| 47 |
|
| 48 |
.. _libxslt: http://xmlsoft.org/XSLT |
| 49 |
|
| 50 |
|
| 51 |
These libraries already ship with Python bindings, but these Python bindings |
| 52 |
mimic the C-level interface. This yields a number of problems: |
| 53 |
|
| 54 |
* very low level and C-ish (not Pythonic). |
| 55 |
|
| 56 |
* underdocumented and huge, you get lost in them. |
| 57 |
|
| 58 |
* UTF-8 in API, instead of Python unicode strings. |
| 59 |
|
| 60 |
* Can easily cause segfaults from Python. |
| 61 |
|
| 62 |
* Require manual memory management! |
| 63 |
|
| 64 |
|
| 65 |
lxml is a new Python binding for libxml2 and libxslt, completely independent |
| 66 |
from these existing Python bindings. Its aims: |
| 67 |
|
| 68 |
* Pythonic API. |
| 69 |
|
| 70 |
* Documented. |
| 71 |
|
| 72 |
* Use Python unicode strings in API. |
| 73 |
|
| 74 |
* Safe (no segfaults). |
| 75 |
|
| 76 |
* No manual memory management! |
| 77 |
|
| 78 |
lxml aims to provide a Pythonic API by following as much as possible the |
| 79 |
`ElementTree API`_. We're trying to avoid inventing too many new APIs, or you |
| 80 |
having to learn new things -- XML is complicated enough. |
| 81 |
|
| 82 |
.. _`ElementTree API`: http://effbot.org/zone/element-index.htm |