Iterate and Assign Object Values from XML in Python

This drove me crazy for the better part of a couple of hours. I had an object defined in Python. Each one of the variables created in the constructor corresponded to an xml tag I needed to search for, and if found, populate the object variable with the contents in the tag.The solution in the example code below was surprisingly simple.

class Node(object):

    def __init__(self):

        # variables named corresponding to xml tags
        self.name = None
        self.cmt = None
        self.desc = None

    def getValues(self, node):

        # for object variable
        for item in vars(self).items():

            # get the value from the xml file
            nodeItem = node.find(item[0])

            # if does not return none, meaning it has a value
            if nodeItem is not None:

                # populate object variable with the xml tag text
                vars(self)[item[0]] = nodeItem.text
In the __init__ constructor, three variables are defined. In my xml file inside of a node, there are three tags I would like the contents of; name, cmt and desc. For this little trick to work, the variable name must exactly match the tag. For my purposes, I like it this way since I remember what it is easier.

It is useful to understand the way Python stores variables of an object. They are little more than dictionary objects. Variables are stored with the variable name as the key and the value as...well...the value.

In line 13 above, vars(self).items() returns a list of variable names for the specified object. In this case, it references the object it resides in, returning a list of dictionary items for the current object. Each variable or dictionary object has two parts, referenced using bracket notation seen in the end of line 16. The first of the two items in each dictionary object is the key, the name of the variable and is located at index 0.

Finding the xml tag is accomplished using ElementTree. The input for this function is a node extracted using either the find or findall function. Within this node, the find function is used to located the tag. The name of this tag, as discussed above, is identical to the name of the variable we are going to be storing it in. Referencing the variable name with the ElementTree find method enables locating the element.

If the tag is found, the line 16 returns an ElementTree object. If it is not found, it simply returns a None value. In line 19, if something is returned and the value is not None, we move to line 22. Line 22 references the text property of the ElementTree object returned in line 16 and assigns it to the object variable, referencing the value of the key using dictionary methods.

The key here is mixing var with dictionary, and sorting out how to do this only took the better part of a morning. Since I am prone to forgetting how to do things, it is all documented in this blog posting so not only you can find it and hopefully make use of it, I can as well! Happy coding and when we are all done, we will go find the nights who say, "NI!"