I have read a lot of noise about ElementTree but never really used it. Tonight i need to extract a bunch of GPS data from a kismet log file. Let’s give it a try
from elementtree import ElementTree data = open('Kismet-May-22-2005-4.xml','r').read() detection = ElementTree.XML(data) for node in detection.getchildren(): try: print "SSID: " + node.find('SSID').text, except AttributeError:pass #hidden SSID print "BSSID: " + node.find('BSSID').text, gps = node.find('gps-info') print "Lon: " + gps.find('max-lon').text + " Lat: " + gps.find('max-lat').text
Really simple, and seems to offer really good speed. I now have another weapon for every day work.
- use ElementTree.parse to read the file (but this returns an ElementTree wrapper, not an Element; use getroot() to get the root element, or use findall() to pick out the right kind of subnodes).
- getchildren() is deprecated; to get all subelements, just loop over the parent container (i.e. “for node in detection” instead of “for node in detection.getchildren()”)
- node.find(tag).text can be written as node.findtext(tag) (however, the latter returns None if no matching element could be found, the former raises an AttributeError exception).
- you can use path syntax to pick out text from subelements (e.g. node.findtext(“gps-info/max-lon”) should work). this is probably slower if you need more than a few subelement texts, though.
Thanks for this comment, Fredrick, I now wondering how to merge 2 Elementree, or choose the best element in a node I gonna read the doc.