-í ¡Ñk9csádZdklZlZlZdkZdkZdkZdeifd„ƒYZzdfd„ƒYZ “dfd„ƒYZ Ôe d jo?Õe ƒZ Öe i eid ƒ o×eid ƒnndS( sThis simple application writes a structured view of the contents of an XML file. The line number after the opening tag demonstrates the use of locator methods. The error and fatalError methods can simply print the exception because the SAXParseException class has a __str__ method. JMS Note: I used these classes as a parse for a simple XML object loading system. I added method calls at the appropriate places so that derived classes can add logic for processing specific XML formats into specific Python classes.(ssaxextsssaxlibssaxutilsNsDocumentHandlercsttZdZd„Z#d„Z'd„Z0d„ZAd„ZId„ZMd„Z Xd„Z RS( s+Handle general document events. This is the main client interface for SAX: it contains callbacks for the most important document events, such as the start and end of elements. You need to create an object that implements this interface, and then register it with the Parser. If you do not want to implement the entire interface, you can derive a class from HandlerBase, which implements the default functionality. You can find the location of any document event using the Locator interface supplied by setDocumentLocator().cs: hgd<dd<dd<|_!d|_dS(Nsnamessindentslinei(sselfs start_tagsdebug(sself((s ./saxparse.pys__init__s'cs#$%||_dS(sAReceive an object for locating the origin of SAX document events.N(slocatorsself(sselfslocator((s ./saxparse.pyssetDocumentLocator#scs_'()d|_*y*+|io,d|iiƒGHnWn-tj o .nXdS(s0Handle an event for the beginning of a document.iÿÿÿÿs Document: %sN(sselfslevelsdebugslocators getSystemIdsAttributeError(sself((s ./saxparse.pys startDocument's  csö012|idƒ3|id|_4d|i|id<5|g|id<9xPt|iƒƒD9]9}:|idi d|i |ƒ|i |ƒfƒqiW<y=|i i ƒ|idtj o?t|ids iN(sselfsoutput_start_tagsdebugslevelsname(sselfsname((s ./saxparse.pys endElementAs  !cs IJKdS(s-Called to handle character data in an elementN((sselfsdata((s ./saxparse.pyshandleCharactersIscsƒMNQti||||!ƒ}R|oRS|i|ƒT|idƒU|i o!Vdd|i d|fGHnndS(sHandle a character data event.sdatas### %s%ss iN( sstringsstripsall_datasstartslengthsdatasselfshandleCharacterssoutput_start_tagsdebugslevel(sselfsall_datasstartslengthsdata((s ./saxparse.pys charactersMs  csXdf|idoùg|ddgjo hd}n-i|dgjo jd}n ltd‚md|id ti|idƒ||f}p|id oqd ||id f}nr|io s|GHnthgd<d d <d d <|_ud Snwt SdS(s¸startElement puts its data in self.start_tag; startElement, characters, and endElement call output_start_tag; when called by startElement or characters and the start tag (of the parent) is still unprinted: print start tag, return 1; else return None; when called by endElement and the start tag is still unprinted: print empty element tag, return 1; else return Nonesnamesstartsdatas>sends/>s&output_start_tag("start"|"data"|"end")s::: %s<%s%s (%s)sindentslines %s (line %s)siN( sselfs start_tagswheresSTAGCs ValueErrorsstringsjoinsoutputsdebugsNone(sselfswheresoutputsSTAGC((s ./saxparse.pysoutput_start_tagXs    0  ' ( s__name__s __module__s__doc__s__init__ssetDocumentLocators startDocuments startElements endElementshandleCharacterss characterssoutput_start_tag(((s ./saxparse.pysDocumentHandlers       s ErrorHandlercsDztZdZƒ„da†d„ZŠd„Zd„ZRS(s Basic interface for SAX error handlers. If you create an object that implements this interface, then register the object with your Parser, the parser will call the methods in your object to report all warnings and errors. There are three levels of errors available: warnings, (possibly) recoverable errors, and unrecoverable errors. All methods take a SAXParseException as the only parameter.sSGML syntax errorcs!†‡ˆtiid|ƒdS(sHandle a recoverable error.s Error: %s N(ssyssstderrswrites exception(sselfs exception((s ./saxparse.pyserror†scs*Š‹Œtiid|ƒt‚dS(sHandle a non-recoverable error.sFatal error: %s N(ssyssstderrswrites exceptionsSGMLSyntaxError(sselfs exception((s ./saxparse.pys fatalErrorŠscs!‘tiid|ƒdS(sHandle a warning.s Warning: %s N(ssyssstderrswrites exception(sselfs exception((s ./saxparse.pyswarnings(s__name__s __module__s__doc__sSGMLSyntaxErrorserrors fatalErrorswarning(((s ./saxparse.pys ErrorHandlerzs    s ParseHandlercs_“tZ•d„Z™d„Zœd„ZŸd„Z»d„Z¾d„ZÃd„ZRS(Ncs•–t|_—t|_dS(N(sNonesselfshandlers SAXparser(sself((s ./saxparse.pys__init__•s cs™štƒSdS(N(sDocumentHandler(sself((s ./saxparse.pys MakeHandler™scsœ|ii|ƒdS(N(sselfs SAXparsersparsesname(sselfsname((s ./saxparse.pys DoSAXParseœscsgŸ¡dkl}¢|iƒ|_¯|iƒ|_±|ii|iƒµ|iit ƒƒdS(N(s drv_xmlproc( sxml.sax.driverss drv_xmlprocs SAX_XPParsersselfs SAXparsers MakeHandlershandlerssetDocumentHandlerssetErrorHandlers ErrorHandler(sselfs drv_xmlproc((s ./saxparse.pysSetupŸs  cs »¼dS(N((sself((s ./saxparse.pys PostParse»scs1¾¿|iƒÀ|i|ƒÁ|iƒdS(N(sselfsSetups DoSAXParsesnames PostParse(sselfsname((s ./saxparse.pysRun¾s cs•ÃÄyÅ|i|ƒWnmÇtj o%ÈtiidtƒÉdSn;Ìtij o(ÍtiidtiƒÏdSnXÐdSdS(Ns%s; processing aborted ii( sselfsRunsnamesSGMLSyntaxErrorssyssstderrswritessaxlibsSAXParseException(sselfsname((s ./saxparse.pys RunAndCatchÃs  ( s__name__s __module__s__init__s MakeHandlers DoSAXParsesSetups PostParsesRuns RunAndCatch(((s ./saxparse.pys ParseHandler“s       s__main__i(s__doc__sxml.saxssaxextsssaxlibssaxutilsssyssurllibsstringsDocumentHandlers ErrorHandlers ParseHandlers__name__s parsehandlers RunAndCatchsargvsexit( sstringsDocumentHandlerssaxextss ErrorHandlers ParseHandlerssysssaxlibsurllibssaxutilss parsehandler((s ./saxparse.pys?s fA