This is my XML File
temp_student.xml
<?xml version='1.0' encoding='UTF-8'?>
<studentinformation doc_version='2.0'>
<student>
<rollno>1</rollno>
<name>ABC</name>
<age>22</age>
<gender>MALE</gender>
</student>
<student>
<rollno>2</rollno>
<name>DEF</name>
<age>56</age>
<gender>MALE</gender>
</student>
</studentinformation>
I'm trying to parse it using Element Tree. I've commented the section of the code to explain my problem
import subprocess,re,os,time,shutil,glob
import sys
from sys import stdout
from subprocess import STDOUT
from _winapi import NULL
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element, SubElement
from logging import root
xmlfile='temp_student.xml'
xml_tree = ET.parse(xmlfile)
tree_root = xml_tree.getroot()
print('Root Tag is '+str(tree_root.tag))
print('Root Attrib is '+str(tree_root.attrib))
print('Printing the children from root ')
# 1 for child_root in tree_root:
# for child_in in child_root:
# perform some operation
# 2 for child_in in child_root in tree_root:
# perform some operation
I thought of trying #2
by putting the entire statement in a single line but it fails because child_root is NOT defined
File "getsccontent.py", line 47, in for child_in_child_root in child_root in tree_root: NameError: name 'child_root' is not defined
Is there any simpler way than writing multiple nested for statements to traverse N ?