Dive into Python: Scripts and Streams –
class KantGenerator:
def _load(self, source):
sock = toolbox.openAnything(source)
xmldoc = minidom.parse(sock).documentElement
sock.close()
return xmldoc
I am curious, why do we have documentElement attribute when –
xmldoc = minidom.parse(sock)
would have given me the same thing as well? (seems both refer to the current instance)
A pydoc query is not giving me much clue either.
$ pydoc xml.dom.minidom.Document.documentElement
xml.dom.minidom.Document.documentElement
Top-level element of this document.
The
minidom.Documentis a xml.dom.Document object, whileminidom.Document.documentElementis an xml.dom.Element object.The
Documentcontains extra things, like encoding, where theElementcontains the actual XML data.There can only be one
Elementin the main document (hencedocumentElement), where an element can have many children of other elements.They both share many methods (both are derived from
xml.dom.Node), so in many cases they can be interchangeable.