Splitting XML file into multiple at given tags

后端 未结 3 1548
孤城傲影
孤城傲影 2021-01-15 11:24

I want to split a XML file into multiple files. My workstation is very limited to Eclipse Mars with Xalan 2.7.1.

I can also use Python, but never used it before.

3条回答
  •  隐瞒了意图╮
    2021-01-15 12:23

    This is the code which works perfect.

    import xml.etree.ElementTree as ET
    
    context = ET.iterparse('filname.xml', events=('end', ))
    for event, elem in context:
    if elem.tag == 'row':
        title = elem.find('NAME').text
        filename = format(title + ".xml")
        with open(filename, 'wb') as f:
            f.write("\n")
            f.write("\n")
            f.write(ET.tostring(elem))
            f.write("")
    

提交回复
热议问题