import xml.etree.ElementTree as et

class Parsexml:

    def xml_check(self, xml, n=2):
        for elem in et.iterparse(xml):
            if 0 < n:
                n -= 1
                print(str(elem[1]).encode('utf-8'))
            else:
                break

    def xml_to_txt(self, xml, txt):
        '''
        if xml is broken, the iterparse will fail,
        so the rest will not in output.
        '''
        for elem in et.iterparse(xml):
            f = open(txt, 'a', encoding='utf-8')
            f.write(str(elem[1].attrib) + '\n')
            f.close()
            print('bingo')

        
if __name__ == '__main__':
    p = Parsexml()
    #p.xml_check('posts.xml', 2)
    p.xml_to_txt('posts.xml', 'posts.txt')

