Iterasjon
Eksempel
""" Iterating an xml-file (SAX-like) Reporting start and end for each element found """ from lxml import etree """ Just dropping out when error """ def testing1(): xmlfile="all_results.xml" try: for event, element in etree.iterparse(xmlfile,events=("start","end")): print "%s, %s, %s" %(event,element.tag,element.text) except: print('sorry') """ With errorhandling """ def testing2(): xmlfile='all_results_with_error.xml' etree.clear_error_log() e=None try: for event, element in etree.iterparse(xmlfile,events=("start","end")): print "%s, %s, %s" %(event,element.tag,element.text) except etree.XMLSyntaxError, e: pass if e != None: log=e.error_log.filter_from_level(etree.ErrorLevels.FATAL) print(log) testing1() #testing2()
testing1() produserer (utdrag)
... start, IOC, start, OlympicGame, start, event, start, athlet, start, name, Dennis Mitchell end, name, Dennis Mitchell start, nation, USA end, nation, USA start, result, 10.04 end, result, 10.04 end, athlet, start, athlet, start, name, Bruny Surin end, name, Bruny Surin start, nation, CAN end, nation, CAN start, result, 10.09 end, result, 10.09 end, athlet, ...
testing2 produserer:
all_results_with_error.xml:6:33:FATAL:PARSER:ERR_TAG_NAME_MISMATCH: Opening and ending tag mismatch: name line 6 and bame
siden vi har brukt en XML-fil som ikke er velformet