Parsing XML in Python: A Quick Working Sample
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.
Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.
__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"?> <catalog> <book id="bk101"> <author>Steve Jin</author> <title>VMware VI and vSphere SDK</title> <genre>Computer</genre> <price>59.99</price> <publish_date>2009-09-01</publish_date> <description> An in-depth introduction on VMware vSphere SDK and open source vijava API. </description> </book> <book id="bk102"> <author>Steve Jin</author> <title>TBD</title> <genre>Computer</genre> <price>59.99</price> <publish_date>2015-09-01</publish_date> <description> An in-depth introduction on something. </description> </catalog>