Posts Tagged Python XML DOM Linux Gentoo Bugzilla tutorial example

Python and XML DOM fun

So I have recently been playing with Java a great deal and recently learned how to parse XML with it. Java is a bit overkill for a multitude of small projects or scripts I want to develop however, so I have decided to scratch my python itch once again. Googling around and looking at the python docs I didn’t really see an example that I could easily follow or adapt. So in the end I took a recent Java project and converted the XML part of it to python to bring life to this little example code. GBug is a tiny little python app that uses DOM to parse XML from the Gentoo Bugzilla and provides some simple information about the requested bug number. I believe the example is pretty clear, although I wrote it in just a few minutes so it could be done better. I hope someone finds the example useful.

#!/usr/bin/env python

Licensed under the GPLv3 by Kenneth Prugh [ken69267@gmail.com] (C) 2008

DOM example adapted from one of my Java projects which displays info about

the desired bug from the Gentoo Bugzilla

from xml.dom import minidom import urllib2 class gbug(): def getValueForTag(self, Element, tag): NodeList = Element.getElementsByTagName(tag) Element = NodeList.item(0) return Element.firstChild.nodeValue def getBug(self, bugID): self.bugID = bugID bugURL = urllib2.urlopen("http://bugs.gentoo.org/show_bug.cgi?ctype=xml&id="+ \ bugID) doc = minidom.parse(bugURL) Element = doc.documentElement bugNodes = doc.getElementsByTagName("bug") bugEL = bugNodes.item(0) print "Description: " + self.getValueForTag(bugEL, "short_desc") print "Reporter: " + self.getValueForTag(bugEL, "reporter") print "Created: " + self.getValueForTag(bugEL, "creation_ts") print "Status: " + self.getValueForTag(bugEL, "bug_status") print "Assigned to: " + self.getValueForTag(bugEL, "assigned_to") main = gbug() input = raw_input("bug #: ") main.getBug(input)

Sample Output:
ken@t61 python % python gbug.py
bug #: 216776
Description: dev-java/smack-3.0.4 version bump
Reporter: ken69267@gentoo.org
Created: 2008-04-07 21:34 0000
Status: NEW
Assigned to: java@gentoo.org
Download gbug

4 Comments