python split large xml file by tag

Solutions on MaxInterview for python split large xml file by tag by the best coders in the world

showing results for - "python split large xml file by tag"
Nick
18 Feb 2018
1import xml.etree.ElementTree as ET
2context = ET.iterparse('file.xml', events=('start' , 'end' ))
3for event, elem in context:
4    if elem.tag == 'row':
5        title = elem.find('NAME').text
6        filename = format(title + ".xml")
7        with open(filename, 'wb') as f:
8            f.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
9            f.write(ET.tostring(elem))
10