Pages

Sunday, December 30, 2012

XML file generation using Python's ElementTree

Introduction

When we want to generate XML file from whatever python data structure, ElementTree might be the easiest solution. For example, when we want to transform a list of list . pairList = [ ["A.txt", "B.txt"] … ] into XML format. <root>
<node fileA="A.txt" fileB="B.txt" />
<node ... />
</root>
</code>

Code

We can implement the code as follows.
import xml.etree.ElementTree as ET

root = ET.Element('root')
for pairs in self.pairList:
    #print pairs
    testa = pairs[0]
    testb = pairs[1]
            
    child = ET.Element('node')
    child.attrib['fileA'] = testa
    child.attrib['fileB'] = testb
    root.append(child)
            
file = open(filePath, 'w')

#Create an ElementTree object from the root element
ET.ElementTree(root).write(file)

Explanations

The API names of ElementTree is pretty straightforward. For getting (or creating) an element, you need to use xml.etree.ElementTree.Element (ET.Element when you import with the name ET) API the tag name as a parameter. When you make the element, you can just append (it's also the API name python uses) the newly generated element as a children of the parent element.

You just keep creating the hierarchy using ET.Element() static and append() API.

The element has "attrib" dictionary to store the attributes of the node.

For writing the result into an XML file, you select the root, and use the write method to print out the XML.

Reference

  1. Python ElementTree XML Tutorial
  2. Processing XML in Python with ElementTree

No comments:

Post a Comment