Source

dataliberation / issues / tigris / io / xml.py

Full commit
'''
Some helper functions for working with xml.dom.minidom classes/definitions
Created on 24.02.2010

@author: dirk
'''

def addChildText(doc, root, cname, text):
    key = doc.createElement(cname)
    t = doc.createTextNode(text)
    key.appendChild(t)
    root.appendChild(key)

def addChildInt(doc, root, cname, i):
    txt = ""
    try:
        txt = str(i)
    except:
        pass
    addChildText(doc, root, cname, txt)
    
def addChildBool(doc, root, cname, b):
    if b:
        addChildText(doc, root, cname, "1")
    else:
        addChildText(doc, root, cname, "0")

def getText(root):
    txt = ""
    for e in root.childNodes:
        if (e.nodeType == e.TEXT_NODE):
            txt += e.data
        if (e.nodeType == e.ENTITY_REFERENCE_NODE):
            print e.data
    return txt

def getChildText(root, cname):
    txt = ""
    for e in root.childNodes:
        if e.nodeType == e.ELEMENT_NODE and e.localName == cname:
            txt += getText(e)
            #txt = str(e.normalize())
            break
    return txt

def getChildInt(root, cname, default=0):
    txt = getChildText(root, cname)
    i = default
    try:
        i = int(txt)
    except:
        pass
    
    return i

bool_texts = ['true', 'True', 'TRUE', '1']

def getChildBool(root, cname, default=False):
    txt = getChildText(root, cname)
    if txt == "":
        return default
    else:
        if txt in bool_texts:
            return True
    
    return False