Следуя примерам в документах , вы можете извлечь все именованные элементы, скажем, вкладчики, и экспортировать их в новый XML-документ.
import xml.dom.minidom as minidom
#open the input csv/xml file
inputPath = '/path/to/xml.csv'
xml_csv = open(inputPath)
#open a output file in write mode
outputPath = '/path/to/contributors.xml'
outxml = open(outputPath,'w')
#create a new xml document and top level element
impl = minidom.getDOMImplementation()
newxml = impl.createDocument(None,'contributors',None)
top = newxml.documentElement
#loop through each line in the file splitting on commas
for line in xml_csv:
xmlFields = line.split(',')
for fldxml in xmlFields:
#double double quotes caused the parser to choke, I'm replacing them here
fldxml = fldxml.replace('""','"')
#parse the xml data from each field and
#find all contributor elements under the top level
dom = minidom.parseString(xmlfld)
contributors = entry.getElementByTagName('contributor')
#add each contributor to the new xml document
for contributor in contributors:
top.appendChild(contributor)
#write out the new xml contributors document in pretty XML
outxml.write(newxml.toprettyxml())
outxml.close()