I have a master xml file called vs_origonal_M.xml I want to add all types of a certain child
<location>
</location>
<location>
</location>
.
.
.
<location>
</location>
until all the files are looked at.
I am doing this by first opening the directory, next I am making a list of all the files in the directory and checking to see if they are indeed xml files, then I am taking a certain child out. Then (Here's where I am stuck) I need to open the master file and insert this child right under the last child of the same name, finally when all done I need to save the master xml file
Here is the code:
# List the xml files in the directory
from xml.dom import minidom
from xml.etree import ElementTree as ET
import glob
import os
import sys
def is_xml(HART_filename):
string_length = len(HART_filename)
suffix = '.xml'
if HART_filename.endswith(suffix):
return True
else:
return False
#add the directory to the python script
os.chdir("c:/Users/ME/Documents/XML_Parasing_Python")
#List all the files in an array
xml_list = os.listdir("c:/Users/ME/Documents/XML_Parasing_Python")
print xml_list
xml_list_length = len(xml_list)
print xml_list_length
number = 1
for number in range(1,xml_list_length):
string_length = len(xml_list[number])
#print string_length
print xml_list[number]
#check to see if file is .xml
if is_xml(xml_list[number]) == True:
xmldoc = minidom.parse(xml_list[number])
reflist = xmldoc.getElementsByTagName('location')
var_ref = reflist[0]
print reflist[0].toxml()
#Add to master .xml file
tree = ET.parse('vs_original_M.xml')
number += 1
else:
number += 1
print 'wasn''t true'
See Question&Answers more detail:os