'Generate XML stream out of the tree structure'

def toXml(self, level, f):

    f.write(('<' + self.tagName))
    if (level == 0):
        for (namespace, prefix) in list(self.namespaces.items()):
            f.write((((('    xmlns:' + prefix) + '="') + _escape(str(namespace))) + '"'))
    for qname in list(self.attributes.keys()):
        prefix = self.get_nsprefix(qname[0])
        f.write(((('    ' + _escape(str(((prefix + ':') + qname[1])))) + '=') + _quoteattr(str(self.attributes[qname]).encode('utf-8'))))
    if self.childNodes:
        f.write('>')
        for element in self.childNodes:
            element.toXml((level + 1), f)
        f.write((('</' + self.tagName) + '>'))
    else:
        f.write('/>')
