'Method used to dump node into textual representation.
if "fancy" argument is set to True produces indented output for readability.'

def __str__(self, fancy=0):

    s = (((((fancy - 1) * 2) * '    ') + '<') + self.name)
    if self.namespace:
        if ((not self.parent) or (self.parent.namespace != self.namespace)):
            if ('xmlns' not in self.attrs):
                s = (s + ('    xmlns="%s"' % self.namespace))
    for key in self.attrs.keys():
        val = ustr(self.attrs[key])
        s = (s + ('    %s="%s"' % (key, XMLescape(val))))
    s = (s + '>')
    cnt = 0
    if self.kids:
        if fancy:
            s = (s + '\n')
        for a in self.kids:
            if ((not fancy) and ((len(self.data) - 1) >= cnt)):
                s = (s + XMLescape(self.data[cnt]))
            elif ((len(self.data) - 1) >= cnt):
                s = (s + XMLescape(self.data[cnt].strip()))
            if isinstance(a, Node):
                s = (s + a.__str__((fancy and (fancy + 1))))
            elif a:
                s = (s + a.__str__())
            cnt = (cnt + 1)
    if ((not fancy) and ((len(self.data) - 1) >= cnt)):
        s = (s + XMLescape(self.data[cnt]))
    elif ((len(self.data) - 1) >= cnt):
        s = (s + XMLescape(self.data[cnt].strip()))
    if ((not self.kids) and s.endswith('>')):
        s = (s[:(-1)] + '    />')
        if fancy:
            s = (s + '\n')
    else:
        if (fancy and (not self.data)):
            s = (s + (((fancy - 1) * 2) * '    '))
        s = (((s + '</') + self.name) + '>')
        if fancy:
            s = (s + '\n')
    return s
