[Tutor] xml parsing from xml
Neil D. Cerutti
neilc at norwich.edu
Wed May 7 20:04:23 CEST 2014
On 5/7/2014 1:39 PM, Alan Gauld wrote:
> On 07/05/14 17:56, Stefan Behnel wrote:
>> Alan Gauld, 07.05.2014 18:11:
>
>>> and ElementTree (aka etree). The documenation gives examples of both.
>>> sax
>>> is easiest and fastest for simple XML in big files ...
>>
>> I wouldn't say that SAX qualifies as "easiest". Sure, if the task is
>> something like "count number of abc tags" or "find tag xyz and get an
>> attribute value from it", then SAX is relatively easy and also quite
>> fast.
>
> That's pretty much what I said. simple task, big file. sax is easy.
>
> For anything else use etree.
>
>> BTW, ElementTree also has a SAX-like parsing mode, but comes with a
>> simpler
>> interface and saner parser configuration defaults.
>
> My experience was different. Etree is powerful but for simple
> tasks I just found sax easier to grok. (And most of my XML parsing
> is limited to simple extraction of a field or two.)
If I understand this task correctly it seems like a good application for
SAX. As a state machine it could have a mere two states, assuming we
aren't troubled about the parent nodes of Country tags.
Using callbacks is a trickier form of programming than ElementTree calls
for, though.
In my own personal case, I partly prefer xml.sax simply because it
ignores namespaces, a nice benefit in my cases. I wish I could make
ElementTree do that.
--
Neil Cerutti
More information about the Tutor
mailing list