Parse XML Document in Python

from xml.etree import ElementTree
#------------------------------------------------------------------------------------------------
# function to parse XML in dictionary
def readConfigFile(filename):
    '''Reads config XLS and returns dictionary of all variables'''
    dictree = ElementTree.parse(filename).getroot()
    global_params = {}
    listCtrl_values = []
    tmpList = []
    relative_paths = ["relativepath”]
    print "Reading Global Parameters..."
    for entry in dictree:
        relative_switch = 0
        tmpList = []
        key = re.sub('[\t\n\r\f\v]','',str(entry.tag))
        value = re.sub('[\t\n\r\f\v]','',str(entry.text))
        for item in relative_paths:
            if key == item:
                relative_switch = 1
        if relative_switch == 1:
            value = os.path.join(os.getcwd(),value)
        global_params[key] = value
        tmpList.append(key)
        tmpList.append(value)
        listCtrl_values.append(tmpList)
    return global_params,listCtrl_values

#------------------------------------------------------------------------------------------------

No comments: