[Tutor] XML parsing with SAX

Kent Johnson kent37 at tds.net
Wed Oct 22 16:25:14 CEST 2008


On Wed, Oct 22, 2008 at 9:07 AM, amit sethi <amit.pureenergy at gmail.com> wrote:
> Sorry , I didn't follow the format of posting to the group. Well I am new to
> xml and python . What I basically want to do is to read elements from the
> xml which is by the way large ~2GB and read the attributes and make a
> decision as to whether i want to keep that element or not . I am told that
> xml.sax is good for reading a large xml file because it can read from a
> stream but what about writing the xml file back  Thank you for your help.

The iterparse() method of ElementTree might be useful, it allows you
to prune the parsed tree as it is built:
http://effbot.org/zone/element-iterparse.htm

The resulting tree can be written back to a file using the write() method:
http://effbot.org/zone/pythondoc-elementtree-ElementTree.htm#elementtree.ElementTree.ElementTree.write-method

Kent


More information about the Tutor mailing list