writing graphics commands to an xml file file open(filename" "file write('\ 'file write('\ 'for cmd in self graphicscommandsfile write('+str(cmd)+"\ " file write('\ 'file close(writing an xml file is like writing any text file except that the text file must conform to the xml grammar specification there are certainly ways to create xml files that differ from how it was presented in sect in the next section we'll learn about xml parsers and very simple way to read xml documents it turns out there are at least some xml frameworks that make writing an xml document just as simple reading xml files xml files would be difficult to read if we had to read them like we read regular text file this is especially true because xml files are not line-oriented they conform to the xml grammarbut the grammar does not specify anything about the lines in the file instead of reading an xml file by reading lines of the filewe use special tool called parser parser is written according to the rules of grammarin this case the xml grammar there are many xml parsers that have been written and different parsers have different features the one we will use in this text is one of the simpler parsers called minidom the minidom parser reads an entire xml file by calling the parse method on it it places the entire contents of an xml file into an sequence of element objects an element object contains the child data and attributes of an xml element along with any other elements that might be defined inside this element to use the minidom parseryou must first import the module where the minidom parser is defined import xml dom minidom thenyou can read an entire xml file by calling the parse method on an xml document as follows xmldoc xml dom minidom parse(filenameonce you have done thatyou can read specific type of element from the xml file by calling the method getelementsbytagname on it for instanceto get the graphicscommands element from the graphics commands xml fileyou would write this graphicscommands xmldoc getelementsbytagname("graphicscommands")[ this copy belongs to 'acha