2
Posted April 2, 2011 by Spyros in Python Programming
 
 

How to Create an XML Writer Class in Python

xml_doc2
xml_doc2

Even if you haven’t had a chance to handle XML through one of your programs yet, chances are that there will be a time that you will need to. Well, maybe that time is now, since you’re reading this post. I have good news for you. Handling XML in Python is very easy. In this post, i will describe how you can create a very handy minimal xml writer class, that will help you create your xml files with ease.

The code is actually pretty short, so i think that i will first paste it here and then discuss how it works :


from xml.dom.minidom import Document
from xml.sax.saxutils import unescape

class XmlWriter:

  def __init__(self):
    self.doc = Document()

  def createNode(self, nodeName, parentNode = '', withAttribs = {}):
    node = self.doc.createElement(nodeName)
    if parentNode == '':   # create a parent node
      createdNode = self.doc.appendChild(node)
    else:                  # create a child node
      createdNode = parentNode.appendChild(node)

    if withAttribs != {}:
      for key, value in withAttribs.items():
        self.setAttribute(createdNode, key, value)

    return createdNode

  def setAttribute(self, node, key, value):
    node.setAttribute(key, value)

  def printXML(self):
    print self.doc.toprettyxml(indent="  ")

As you can see, i’m creating an XMLWriter class, that is responsible for creating xml files. I will be using the nice xml minidom package, which is a very nice and simple way to parse and write xml files. With this xml writer, the process of creating an xml file will be even more elegant and the used code will better illustrate what is happening.

In the class contructor (__init__), we create an instance variable named doc. In this one, we execute Document(), to create a Document object and start populating it with nodes and attributes. Our first interesting function is createNode. This one retrieves a node name and has 2 more optional arguments. The first is parentNode and the second one is withAttribs, that actually expects a python dictionary.

A node in XML can either have a parent and be a child, or be a parent node on its own. If we specify a parentNode, then our newly created node will have to be a child of that node. You can see in the actual code of the function that if parentNode is not specified, we create a new node on the document object. Thus, it’s a parent node. If parentNode is specified, though, we create a node that is a child of that one.

A node can also have certain attributes, like <node attrib1 = ”/>. The withAttribs parameters is a way to specify if you want to add attributes to a node or not. If you do not specify a new dict, the newly created node will not have any attributes. If you do, then you just have to pass a dictionary with keys, values that represent the node attributes. The createNode method utilizes setAttribute, which is the actual function responsible for creating attributes for an XML node. You can make this one private if you like, but i prefer to have the ability to execute it as a standalone method as well.

Finally, there is the printXML method that just outputs the resulting XML. Let’s now take a look at an execution example of the class :


doc = XmlWriter()
node = doc.createNode('node1', withAttribs = {'1': 'first', '2': 'second'})
node2 = doc.createNode('node2', node, withAttribs = {'3': 'third'})
node3 = doc.createNode('node3', node, withAttribs = {'4': 'fourth'})
doc.printXML()

You see that we create a parent node with attribs 1= ‘first’ and 2=’second’. Then, we create a child node of the parent node and we name it node2. This one has attrib 3 = ‘third’. Finally, we create another child to the parent node, with attribs 4 = ‘fourth’. Upon execution of the above, we get this output :

<?xml version="1.0" ?>
<node1 1="first" 2="second">
  <node2 3="third"/>
  <node3 4="fourth"/>
</node1>

Of course, you can redirect that to a file like “python XmlWriter.py > example.xml” or just write a class method that will create the file for you. Hope this makes your xml python creation a bit easier now. In a future post i will also create an xml reader class in Python.


Spyros