Python The xml.dom Module

0
xml dom python module

XML document is now known as a standard of the storing and Transporting data. One of the concept to work with the XML is to know about the XML Dom so-called the XML Document object model. Python Provides the xml.dom Module to make it easy to work with the XML Document object model.

What is XML?

XML is a Markup language, specially designed for storing and transferring data over the internet. XML stands for eXtensible Markup Langauge and is a text-based format for representing information.

Information that can be represented by the XML are documents, data, configuration, transactions, invoices, books and much more. Before the XML we were using SGML, but to make SGML suitable for web uses, it was modified to make XML.

XML Document Object Model

XML is internally represented as the Document Object model and is an in-memory representation of the XML document. When the XML document is loaded to the memory it is represented as an XML Object Model. The Document object model, make it easy to do operation on the XML. The operation that we can do on XML is to read, manipulate and modify the XML document with the help of the Document object model.

Document Object Model
Photo Credit: CodeVoila

What is The Differnce Between XML DOM and SAX?

XML DOM and SAX are both used for XML parsing. XML DOM stands for XML Document Object Model, while SAX stands for Simple API for XML. The main differnce between the XML DOM and SAX is that XML DOM parser load full XML file in the memory and create a tree-like representaiono of XML. SAX is an event basesd XML parser.

Python: xml.dom Module

DOM known as Document object model is a standard API by W3C for accessing and modefiying the XML documents. Python provide a built-in Module to work with XML DOM called the xml.dom module.

registerDOMImplementation(name, factory):

Register the factory function with the name. The factory function should return an object which implements the DOMImplementation interface. The factory function can either return the same object, or a new one.

We basically enter a factory into the dictionary, the key will be the name. following is how this funciton is created.

def registerDOMImplementation(name, factory):
    registered[name] = factory

Remove comment from XML element with Python

We can remove the comment from an xml element by using the following code.

def removeComments(element):

    global _num_bytes_saved_in_comments
    num = 0

    if isinstance(element, xml.dom.minidom.Comment):
        _num_bytes_saved_in_comments += len(element.data)
        element.parentNode.removeChild(element)
        num += 1
    else:
        for subelement in element.childNodes[:]:
            num += removeComments(subelement)

    return num 
You might also like to see

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !