BadPython.com

XML slicer

        
def getAllNodes(text, tag ):
    """
    text = plain text list of lines XML input
    tag = node(s) to get
    returns dict {k: [nodes text] }
        where k = seqential # of node as found in text
    """
    nodes={}
    inf = 0
    nt = []
    for i in range(len(text)):
        mt = re.search('<'+tag+'\\b', text[i] )
        et  = re.search('</'+tag+'>', text[i] )
        et2 = re.search('/>', text[i] )
        if mt: inf = 1
        if inf: nt.append( text[i] )
        if et or (mt and et2):
            nodes[len(nodes)] = nt
            nt=[]
            inf=0
        # endif
    # endfor
    return nodes