Iterasjon
De aktuelle dataene er ordnet i en XML-fil: results.xml
Kopier datafila og pythonkoden nedenfor og eksperimenter.
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="../lxml/all_results.xml" try: for event, element in etree.iterparse(xmlfile,events=("start",)): print ("%s, %s, %s" %(event,element.tag,element.text)) except: print('sorry') """ With errorhandling """ def testing2(): xmlfile='../lxml/all_results_with_error.xml' etree.clear_error_log() try: for event, element in etree.iterparse(xmlfile,events=("start",)): print ("%s, %s, %s" %(event,element.tag,element.text)) except etree.XMLSyntaxError as er: log=er.error_log.filter_from_level(etree.ErrorLevels.FATAL) print(log) #testing1() testing2()
testing1() produserer (starten)
start, IOC, start, OlympicGame, start, event, start, athlet, start, name, Dennis Mitchell start, nation, USA start, result, 10.04 start, athlet, start, name, Bruny Surin start, nation, CAN start, result, 10.09 start, athlet, ...
testing2 produserer:
start, IOC, start, OlympicGame, start, event, start, athlet, start, name, Dennis Mitchell start, nation, USA start, result, 10.04 start, athlet, start, name, Bruny Surin ../lxml/all_results_with_error.xml:11:29:FATAL:PARSER:ERR_TAG_NAME_MISMATCH: Opening and ending tag mismatch: name line 11 and bame
siden vi har brukt en XML-fil som ikke er velformet