Basic XML Processing with Python

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();
  1. doc.createElementNS(self.NS, self.XMl_CONFIGURATIONS); creates an XML element.
  2. root_element.appendChild([an element]); appends an element as a child element
  3. We use the standard open(), write(), and close() file operation to write the XML file>
  4. 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.

Share

About these ads

About zainul franciscus

Hi, I am a software developer and tech blogger who is currently living in Wellington, New Zealand. I write stuff that I find interesting on
This entry was posted in programming, Python, Ubuntu and tagged , . Bookmark the permalink.

2 Responses to Basic XML Processing with Python

  1. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s