0

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 ?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you . but i can add the products to the array only with numpy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.