lxml etree question

Carl Banks pavlovevidence at gmail.com
Sun Dec 26 03:21:33 EST 2010


On Dec 24, 10:17 am, Adam Tauno Williams <awill... at whitemice.org>
wrote:
> On Fri, 2010-12-24 at 20:48 +0530, Nitin Pawar wrote:
> > On Fri, Dec 24, 2010 at 8:40 PM, Jim <jim.heffe... at gmail.com> wrote:
> >         Hello,  I wonder if someone knows about lxml.etree and
> >         namespaces?
>
> Yes, and don't.

He's using lxml.etree (which is a third-party library that mimics
ElementTree's interface), not ElementTree.  Were you aware of this?


> > I want to build an ElementTree where some of the sub-elements have
> > attributes that serialize this way.
> > <comment xml:lang='de'>..</comment>
> > I've tried just comment_elet.set('xml:lang','de') and it didn't like
> > that at all (although it takes comment_elet.set('auth:id','jones')
> > just fine).  I've also spelunked the docs and googled but have not hit
> > on the right invocation.  If someone knows, I'd be grateful.
>
> I'd *strongly* recommend using ElementFlow for building XML documents
> (over ElementTree), especially if namespaces are involved.  ElementFlow
> is far more intuitive.
> <http://pypi.python.org/pypi/elementflow>

I'd have to disagree with the use of strong recommendation here.  The
library you recommended isn't a general replacement for lxml (or
ElementTree), and you didn't qualify the conditions for when it is a
suitable alternative.

A. What if he needed to keep the tree in memory?
B. This library builds the tags with "with" statements, which could be
convenient for xml files with rigid structure, but I would think it'd
be inconvenient if the format were relatively loose.

If you're going to recommend a more specialized solution, you should
also give the conditions for which it is suitable.


Carl Banks



More information about the Python-list mailing list