Reputation: 103
im trying to read xml file to array or list the xml file has multiple child elements :
<Items>
<Company CompanyName="company name">
<Category CategoryName="main category name" />
<Category CategoryName="sub category name">
<Product MAKAT="SKU">
<Name>name</Name>
<Price>XXX</Price>
<IsInStock>Yes</IsInStock>
<URL>url</URL>
</Product>
</Category>
</Category>
</Company>
i have tried to do this :
tree = ET.parse(xmlFile)
root = tree.getroot()
products=[]
for item in tree.findall('Company'):
print(item.attrib)
for subitem in tree.findall('CategoryName'):
print(subitem.attrib)
but i cant reach to all the elements.
i want to reach to all child elements, and insert them into one array . what is the best way to do it in python ?
Upvotes: 0
Views: 558
Reputation: 939
You can iterate through all children by using the method iter(), like this:
for elem in root.iter():
print(elem.tag)
If you want to store the elements in array products, you can do this with numpy: be sure to use np.array([elem])
in order to make np.append()
work.
Upvotes: 1