Python comes with ‘xml.dom.minidom’ a lightweight DOM implementation that we can use to read and write XML file. I am using this to read and write XML file in my Ubuntu. It work seamlessly with Ubuntu 9.10.
Writing XML File
root_element = doc.createElementNS(self.NS, self.XMl_CONFIGURATIONS); config_element = doc.createElementNS(self.NS, self.XML_CONFIG); root_element.appendChild(config_element); description_element = doc.createElementNS(self.NS, self.XML_DESCRIPTION); description_element.appendChild(doc.createTextNode(self.description)); config_element.appendChild(description_element); doc.appendChild(root_element); f = open(self.CONFIG_XML, 'w'); f.write(doc.toprettyxml(indent=" ")); f.close();
- doc.createElementNS(self.NS, self.XMl_CONFIGURATIONS); creates an XML element.
- root_element.appendChild([an element]); appends an element as a child element
- We use the standard open(), write(), and close() file operation to write the XML file>
- doc.toprettyxml(indent=” “); writes a pretty XML with proper indentation for readability
Reading XML File
Reading the XML file is pretty straighforward:
1. Create a dom object:
dom = xml.dom.minidom.parse(self.CONFIG_XML);
2. Read the XML element by using the getElementsByTagName:
dom.getElementsByTagName(the element name)[0].childNodes[0].nodeValue
The code above read the first child value from the specified ‘element name’.
The returned string will contain the spacing and indetation that the parser use to write the pretty XML. I have not found a good way to string these extra characters. I use the following code to remove the extra indentation.
textwrap.dedent(element value).replace('\n','');
note: element value is the XML value that we extracted using getElementsByTagName
Text Wrapper dedent method will take care of the indentation. The replace() method take care of the carriage return characters.






This is cool. I think Python’s support for XML processing is much better and easier to use than those of other languages. I used JDOM with Java and found it a bit tricky and complex, but working with Python is a breeze.
Yup, working with XML in python is easier compare to java. Python simple syntax makes it easier to write shorter code compare to java. Thank you for sharing your thoughts on my Blog.