busting-out XML sections

Martin von Loewis loewis at informatik.hu-berlin.de
Sun Oct 8 12:50:54 EDT 2000


Thomas Gagne <tgagne at ix.netcom.com> writes:

> Niether solution sounds appealing.  It amounts to a lot of code for
> what's really a simple problem.  Maybe there's something in the SAX
> stuff that would allow me to grab everything between (and including)
> the <order> tags.

Using PyXML, I would inherit from XMLGenerator. Roughly, it would look like

class DevNull:
  def write(*args):
    pass

class OrderFilter(XMLGenerator):
  def __init__(self,outstream):
    self._null = DevNull() 
    self._realout = outstream
    XMLGenerator.__init__(self,self._null)

  def startElement(self,tag,attrs):
    XMLGenerator.startElement(tag,attrs)
    if tag == "order":
      self._out = self._realout

  def endElement(self,tag):
    if tag == "order":
      self._out = self._null
    XMLGenerator.startElement(tag,attrs)

Then, create a parser, and attache an OrderFilter created with the
output stream.

Hope this helps,
Martin



More information about the Python-list mailing list