Home > Software Development > Parsing XML in Python: A Quick Working Sample

Parsing XML in Python: A Quick Working Sample

December 22nd, 2013 Leave a comment Go to comments

In my recent consulting projects, I used Python on various projects including the VMware pyVmomi () for managing vSphere. Because XML is ubiqutous these days, I had to use XML for configuration, passing information, etc. Parsing XML is a very basic part of using XML. The following sample code can achieve the same thing (print out may be different) as the C# sample that parses XML I wrote before.

Because the code uses the cElementTree that is backed up by native C implementation, it runs way faster and uses much less memory than its portable equivalent ElementTree. Both cElementTree and ElementTree share the same interfaces, you can just change the “cElementTree” to “ElementTree” in the import statement.

Time to learn how to "Google" and manage your VMware and clouds in a fast and secure

__author__ = 'Steve Jin'
from xml.etree import cElementTree as ET
class Book:
    id = None
    title = None
    author = None
    def __str__(self):
        return self.id + ": " + self.title + " by " + self.author
tree = ET.parse("books.xml")
catalog = tree.getroot()
for node in catalog:
    book = Book()
    book.author = node.find('author').text
    book.title = node.find('title').text
    book.id = node.attrib["id"]
    print book

The books.xml is exactly the same as the previous article.

<?xml version="1.0"?>
  <book id="bk101">
    <author>Steve Jin</author>
    <title>VMware VI and vSphere SDK</title>
      An in-depth introduction on VMware vSphere SDK and open source vijava API.
  <book id="bk102">
    <author>Steve Jin</author>
      An in-depth introduction on something.
Categories: Software Development Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.