This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author amaury.forgeotdarc
Recipients amaury.forgeotdarc, loewis, nemeskeyd
Date 2012-09-05.16:33:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1346862802.56.0.69160861558.issue15775@psf.upfronthosting.co.za>
In-reply-to
Content
Below is a sample script that shows that it's possible to stop parsing XML in the middle, without an explicit call to XML_StopParser(): raise StopParsing from any handler, and catch it around the Parse() call.

This method covers the two proposed use cases.  Do we need another way to do it?


import xml.parsers.expat

class StopParsing(Exception):
    pass

def findFirstElementByName(data, what):
  def end_element(name):
      if name == what:
          raise StopParsing(name)

  p = xml.parsers.expat.ParserCreate()
  p.EndElementHandler = end_element

  try:
      p.Parse(data, True)
  except StopParsing as e:
      print "Element found:", e
  else:
      print "Element not found"

data = """<?xml version="1.0"?>
         <parent id="top"><child1 name="paul">Text goes here</child1>
         <child2 name="fred">More text</child2>
         </parent>"""
findFirstElementByName(data, "child2")   # Found
findFirstElementByName(data, "child3")   # Not found
History
Date User Action Args
2012-09-05 16:33:22amaury.forgeotdarcsetrecipients: + amaury.forgeotdarc, loewis, nemeskeyd
2012-09-05 16:33:22amaury.forgeotdarcsetmessageid: <1346862802.56.0.69160861558.issue15775@psf.upfronthosting.co.za>
2012-09-05 16:33:22amaury.forgeotdarclinkissue15775 messages
2012-09-05 16:33:21amaury.forgeotdarccreate