Message169879
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 |
|
Date |
User |
Action |
Args |
2012-09-05 16:33:22 | amaury.forgeotdarc | set | recipients:
+ amaury.forgeotdarc, loewis, nemeskeyd |
2012-09-05 16:33:22 | amaury.forgeotdarc | set | messageid: <1346862802.56.0.69160861558.issue15775@psf.upfronthosting.co.za> |
2012-09-05 16:33:22 | amaury.forgeotdarc | link | issue15775 messages |
2012-09-05 16:33:21 | amaury.forgeotdarc | create | |
|