Differences between revisions 6 and 7
Revision 6 as of 2011-02-26 09:37:21
Size: 1501
Editor: StefanBehnel
Comment:
Revision 7 as of 2011-02-26 09:38:25
Size: 1509
Editor: StefanBehnel
Comment:
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
'''NOTE''': A faster and much simpler way to extract information from an XML document in an event-driven, memory efficient fashion is [[http://docs.python.org/library/xml.etree.elementtree.html#xml.etree.ElementTree.iterparse | ElementTree.iterparse()]]. '''NOTE''': A similarly fast but much simpler way to extract information from an XML document in an event-driven, memory efficient fashion is [[http://docs.python.org/library/xml.etree.elementtree.html#xml.etree.ElementTree.iterparse | ElementTree.iterparse()]].

"Sax" is an XML parser that operates element by element, line by line.

MiniDom sucks up an entire XML file, holds it in memory, and lets you work with it. Sax, on the other hand, emits events as it goes step by step through the file.

NOTE: A similarly fast but much simpler way to extract information from an XML document in an event-driven, memory efficient fashion is ElementTree.iterparse().

Example

   1 import xml.sax
   2 
   3 class InkscapeSvgHandler(xml.sax.ContentHandler):
   4     def startElement(self, name, attrs):
   5         if name == "svg":
   6             for (k,v) in attrs.items():
   7                 print k + " " + v
   8 
   9 parser = xml.sax.make_parser()
  10 parser.setContentHandler(InkscapeSvgHandler())
  11 parser.parse(open("svg.xml","r"))

Sax (last edited 2011-02-26 09:38:25 by StefanBehnel)

Unable to edit the page? See the FrontPage for instructions.