Elegant XML parsing using the ElementTree Module

Mark Mruss

Note: This article was first published the October 2007 issue of Python Magazine

XML is everywhere. It seems you can’t do much these days unless you utilize XML in one way or another. Fortunately, Python developers have a new tool in our standard arsenal: the ElementTree module. This article aims to introduce you to reading, writing, saving, and loading XML using the ElementTree module.

  1. Introduction
  2. Reading XML data
  3. Listing 1
  4. Listing 2
  5. Reading XML Attributes
  6. Writing XML
  7. Listing 3
  8. Writing XML Attributes
  9. Reading XML Files
  10. Writing XML Data to a File
  11. Reading from the Web
  12. Conclusion

Continue reading Elegant XML parsing using the ElementTree Module

WordPy 0.2 – Using XML to Save and Load Data

All right, so we have our base WordPy application running, so let’s try to extend it a bit more by letting you load and save blog posts to and from an xml file. Please note that this tutorial simply shows one method of saving and loading data using xml, there are many different methods and this method was chosen for its simplicity.

If you are unfamiliar with the first WordPy tutorial you should probably read it fist in order to have a better understanding of some of what happens in this tutorial.

You can download the complete code for this tutorial here.


The first thing we need to-do is open up the wordpy glade project and make some changes:

  1. We’ll start off by adding another item to our VBox in Glade. You can do this by holding down shift and clicking on the WordPy window until you see the GTKVBox come up in the properties window. Then simply change it’s size value from 4 to 5.
  2. In the empty space add a menu bar. Then on the Packing tab of the menu bar’s properties set the position to be zero, so that the menu is at the top of the window.
  3. Then edit the menu so that only the File, Edit, and Help menu’s remain.
  4. Add handlers to each of the files menu items: on_file_new, on_file_open, on_file_save, on_file_save_as

GLADE Window PyWine

The Code

That’s it for editing the GUI, now we have to go and edit the code. The first step is to connect all of the menu events with our code:

[code lang=”python”]
dic = {“on_wndMain_destroy” : self.quit
, “on_btnBold_clicked” : self.on_btnBold_clicked
, “on_btnItalic_clicked” : self.on_btnItalic_clicked
, “on_btnLink_clicked” : self.on_btnLink_clicked
, “on_btnBlockQuote_clicked” : self.on_btnBlockQuote_clicked
, “on_btnDel_clicked” : self.on_btnDel_clicked
, “on_btnIns_clicked” : self.on_btnIns_clicked
, “on_btnImage_clicked” : self.on_btnImage_clicked
, “on_btnUnorderedList_clicked” : self.on_btnUnorderedList_clicked
, “on_btnOrderedList_clicked” : self.on_btnOrderedList_clicked
, “on_btnListItem_clicked” : self.on_btnListItem_clicked
, “on_btnCode_clicked” : self.on_btnCode_clicked
, “on_btnMore_clicked” : self.on_btnMore_clicked
, “on_btnSettings_clicked” : self.on_btnSettings_clicked
, “on_btnpost_clicked” : self.on_btnpost_clicked
, “on_file_new” : self.on_file_new
, “on_file_open” : self.on_file_open
, “on_file_save” : self.on_file_save
, “on_file_save_as”: self.on_file_save_as}

[code lang=”python”]
def on_file_new(self, widget):

def on_file_open(self, widget):

def on_file_save(self, widget):

def on_file_save_as(self. widget):

Continue reading WordPy 0.2 – Using XML to Save and Load Data