[Tutor] parsing XML

Alan Gauld alan.gauld at btinternet.com
Tue Nov 10 06:53:39 CET 2009


"Christopher Spears" <cspears2002 at yahoo.com> wrote 

> I need to parse several XML documents into a Python dictionary.  
> Is there a module that would be particularly good for this?  
> I heard beginners should start with ElementTree.  
> However, SAX seems to make a little more sense to me.  

XML parsers fall into 2 groups. Those that parse the whole 
structure and create a tree of objects - usually accessed like 
a dictionary, and those that parse line by line looking for patterns. 
ElementTree is of the former, sax of the latter.

The former approach is usually slightly slower and more resource 
hungry but is much more flexible. SAX is fast but generally best 
if you only want to read something specific out of the XML.

If SAX makes sense for you and meets your needs go with it.

But ElementTree is worth persevering with if you need to do 
more complex editing of the XML. Its certainly easier than minidom.
(The other standard tree parser in Python)

Alan G.



More information about the Tutor mailing list