I want to get a first child of a XML document (without knowing the exact names of the nodes), multi levels deep.
I'm porting some code from JS to Python. This was the JS code:
let document = XmlService.parse(response);
let root = document.getRootElement();
return root.getContent(0).getContent(0).getContent(0).getContent(0);
I'm using ElementTree in Python, I now have this:
tree = ElementTree(fromstring(response.text))
root = tree.getroot()
print(list(root.iter())[0].tag)
This gets the tag of the first element, good. But when I try to get deeper, like this:
print(list(list(root.iter())[0].iter())[0].tag)
Pretty ugly code, but this line produces the same as the previous one. The tag of the first element at level 1 is shown, not the tag of the first element at level 2.
Any help or guidance is very much appreciated.
The code from @Chandan Maurya showed that I can just do rootElement[0]. So I just did this:
And this works. A recursive function is not needed in my case because I know how deep I need to go.