XML (Extensive markup language) is a type of markup language that mainly provides small to medium amounts of data without need of SQL-type database.
The Python supports two extensive usages of APIs to process XML data like SAX and DOM interfaces.
SAX (Simple API for XML): It parses the XML file from saved disk but the whole file is not saved into the memory. So, this parser is useful for larger documents and restricted memory size. The Syntax for creating a new parser is as follows,
xml.sax.make_parser( [parser_list] )
This method creates a new parser object and the optional parameter, parser_list contain list of parsers that would be utilized.
To create a SAX parser, the following method is used,
xml.sax.parse( xmlfile, contenthandler[, errorhandler])
where, xmlfile is the XML filename whose contents should be read, contenthandler denotes an object of ContentHandler and errorhandler denotes as SAX ErrorHandler object.
For parsing particular xml string, another method to create a SAX parser is as below,
xml.sax.parseString(xmlstring, contenthandler[, errorhandler])
where, xmlstring denotes the XML string_name which is to be read, contenthandler denotes an object of ContentHandler and errorhandler denotes as SAX ErrorHandler object.
For using the above methods, import xml.sax should be required.
DOM (Document object model) API : In this case, the whole file is read into memory as well as stored into a tree-like structure for representing all features of XML. It permits to change in the XML file whereas SAX is read-only. The following Syntax is used to parse the XML file of the minidom object.
from xml.dom.minidom import parse
# Open XML document using minidom parser
DOMTree = xml.dom.minidom.parse("xm_filenamel")