I am using Element Tree to parse XML files. I have multiple XML files. The elements of the XML files are identified by a unique key (SKU), but other tags are different. I want to combine the tags corresponding to each element in another file. To do this I can start parsing each child element of the first XML and loop through the child elements of other files to find the element with the give sku:
tree = ET.parse(filename)
root = tree.getroot()
tree1 = ET.parse(filename1)
root1 = tree1.getroot()
...#more xmls
for child in root:
sku = child.find('SKU').text
for child1 in root1:
sku1 = child1.find('SKU').text
if sku == sku1:
#do something
But I realize that this method is not very efficient. Is there a better way of doing this?
Thanks
EDIT: Eg. the 1st xml has elements of the following form:
<product>
<SKU>ABCD1234</SKU>
<_Image>something</_Image>
<_Image_Count>2</_Image_Count>
<_Image2>something</_Image2>
<_Image3>something</_Image3>
<_Orignal_Image>something</_Orignal_Image>
</product>
and 2nd XML has the elements of the following form:
<product>
<Product_Code>ABCD1234</Product_Code>
<Designer>xxx</Designer>
<Taxon>yyy</Taxon>
<Parent_Taxon>zzz</Parent_Taxon>
<Taxonomy>aaa</Taxonomy>
<Quantity>1</Quantity>
<Cost>2</Cost>
<MRP>3</MRP>
<Price>4</Price>
</product>
I want to combine the 2 XMLs to get:
<product>
<SKU>ABCD1234</SKU>
<_Image>something</_Image>
<_Image_Count>2</_Image_Count>
<_Image2>something</_Image2>
<_Image3>something</_Image3>
<_Orignal_Image>something</_Orignal_Image>
<Product_Code>ABCD1234</Product_Code>
<Designer>xxx</Designer>
<Taxon>yyy</Taxon>
<Parent_Taxon>zzz</Parent_Taxon>
<Taxonomy>aaa</Taxonomy>
<Quantity>1</Quantity>
<Cost>2</Cost>
<MRP>3</MRP>
<Price>4</Price>
</product>