Skip to content Skip to sidebar Skip to footer

Iterate Over Xml Response With Elementtreee Django

My goal is to take the xml response and save it to my database. I was able to access the first element with Elementtree which was products The xml looks like this,

Solution 1:

As mentioned in the documentation, iterparse() returns (event, elem) pairs (notice the order). Your code has event and elem variables in the wrong order, that's why it always print end from "end" event. Correct the ordering, then you can check current element name from elem.tag and get value of the element from elem.text :

for event, elem in items:
    print(elem.tag, elem.text)

Post a Comment for "Iterate Over Xml Response With Elementtreee Django"