internet data handling and encoding utility functions the following utility functions are defineddump(elemdumps the element structure of elem to sys stdout for debugging the output is usually xml iselement(elemchecks if elem is valid element object iterparse(source [events]incrementally parses xml from source source is filename or file-like object referring to xml data events is list of event types to produce possible event types are 'start''end''start-ns'and 'end-nsif omittedonly 'endevents are produced the value returned by this function is an iterator that produces tuples (eventelemwhere event is string such as 'startor 'endand elem is the element being processed for 'starteventsthe element is newly created and initially empty except for attributes for 'endeventsthe element is fully populated and includes all subelements parse(sourcefully parses an xml source into an elementtree object source is filename or filelike object with xml data tostring(elemcreates an xml string representing elem and all of its subelements xml examples here is an example of using elementtree to parse the sample recipe file and print an ingredient list it is similar to the example shown for dom from xml etree elementtree import elementtree doc elementtree(file="recipe xml"ingredients doc find('ingredients'for item in ingredients findall('item')num item get('num'units item get('units',''text item text strip(quantity "% % (numunitsprint("%- % (quantitytext)the path syntax of elementtree makes it easier to simplify certain tasks and to take shortcuts as necessary for examplehere is different version of the previous code that uses the path syntax to simply extract all elements from xml etree elementtree import elementtree doc elementtree(file="recipe xml"for item in doc findall(//item")num item get('num'units item get('units',''text item text strip(quantity "% % (numunitsprint("%- % (quantitytext) lib fl ff