Using ElementTree in Python

June 17, 2010

This page contains some code that is useful for working with ElementTree in Python.

Parse an XML File

This code parses an XML file from disk into an ElementTree instance

from xml.etree.ElementTree import ElementTree

lDocument = ElementTree()
lRoot = lDocument.getroot()

Parse from String

This code parses to an XML document from a string, and returns the root node.

from xml.etree.ElementTree import fromstring

lRoot = fromstring(lXmlAsString)

Get Child Tags

This is an example of getting hold of the child tags

if child.tag == 'ResourceParams':
    lChildren = child.getchildren()

Create If Not Present

This code looks for a given tag (Environment) with a given attribute (name="username") and creates it if it does not exist

lFound = False
for child in lRoot.getchildren():
    if child.tag == 'Environment':
        if child.attrib['name'] == 'Username':
            lFound = True

if not lFound:
    lRoot.append(Element('Environment', {'description': 'Username', 'name':'Username'}))

Get Attribute Value

This code gets hold of the value of an attribute

for child in lRoot.getchildren():
    if child.tag == 'Environment':
        lUrl = child.get('value')

Update Attributes

Update attributes in a parsed XML document

for child in lRoot.getchildren():
    if child.tag == 'Environment':
        if child.attrib['name'] == 'AmqpUsername':
            child.set('value', lRabbitUsername)
        elif child.attrib['name'] == 'AmqpPassword':
            child.set('value',  lRabbitPassword)
        elif child.attrib['name'] == 'AmqpVirtualHost':
            child.set('value', lRabbitVirtualHost)
        elif child.attrib['name'] == 'AmqpHost':
            child.set('value', lRabbitHost)
        elif child.attrib['name'] == 'AmqpPort':
            child.set('value', lRabbitPort)
        elif child.attrib['name'] == 'AmqpExchangePrefix':
            child.set('value', lRabbitExchangePrefix)

Update Tag Text Value

This code is an example of updating the text value in an XML tag

for child in lPoolSizeRoot.getchildren():
    if child.tag == 'ResourceParams':
        lParameters = child.getchildren()
        for lParam in lParameters:
            if lParam.find('name').text == 'maxActive':
                lParam.find('value').text = str(lPoolSize)

Deleting Elements

Don't delete elements whilst iterating through the document (such as from a call to getchildren()). This will cause tags to be skipped by the iterator. Instead, add them to a delete list then use delete() after you've finished iterating the loop:

for tag in lRemoveList:

Write XML File

This code writes back the XML file to disk

from xml.etree.ElementTree import tostring

lXml = tostring(lDocument.getroot(),'utf-8')
lXml = "<?xml version='1.0' encoding='utf-8'?>\n%s" % lXml
lFile = open(pFilepath, "w")