titles by exampleusing each of the four primary python tools at our disposal--patternssaxdomand elementtree regular expression parsing in some contextsthe regular expressions we met earlier can be used to parse information from xml files they are not complete parsersand are not very robust or accurate in the presence of arbitrary text (text in tag attributes can especially throw them offwhere applicablethoughthey offer simple option example - shows how we might go about parsing the xml file in example - with the prior section' re module like all four examples in this sectionit scans the xml file looking at isbn numbers and associated titlesand stores the two as keys and values in python dictionary example - pp \lang\xml\rebook py ""xml parsingregular expressions (no robust or general""import repprint text open('books xml'read(pattern '(? )isbn="*?)*?*?)found re findall(patterntextmapping {isbntitle for (isbntitlein foundpprint pprint(mappingstr if str pattern *?=nongreedy (? )=dot matches / dict from tuple list when runthe re findall method locates all the nested tags we're interested inextracts their contentand returns list of tuples representing the two parenthesized groups in the pattern python' pprint module displays the dictionary created by the comprehension nicely the extract worksbut only as long as the text doesn' deviate from the expected pattern in ways that would invalidate our script moreoverthe xml entity for "&in the first book' title is not un-escaped automaticallyc:\pp \lang\xmlpython rebook py {'- ''python &ampxml''- ''python cookbook nd edition''- ''python in nutshell nd edition''- ''learning python th edition''- ''python pocket reference th edition''- ''programming python th edition'sax parsing to do betterpython' full-blown xml parsing tools let us perform this data extraction in more accurate and robust way example - for instancedefines sax-based parsing procedureits class implements callback methods that will be called during the parseand its top-level code creates and runs parser xml and html parsing