使用Python ElementTree。
创建一个文件,例如xmlsplitter.py。在下面添加代码(其中file.xml是您的xml文件,并假设每一行都有一个唯一的NAME元素。)。
import xml.etree.ElementTree as ETcontext = ET.iterparse('file.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("<?xml version="1.0" encoding="UTF-8"?>n") f.write(ET.tostring(elem))
运行它
python xmlsplitter.py
或者,如果名称不是唯一的:
import xml.etree.ElementTree as ETcontext = ET.iterparse('file.xml', events=('end', ))index = 0for event, elem in context: if elem.tag == 'row': index += 1 filename = format(str(index) + ".xml") with open(filename, 'wb') as f: f.write("<?xml version="1.0" encoding="UTF-8"?>n") f.write(ET.tostring(elem))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)