My XML looks like this
<TOPIC>
<LIST>
<Area>JKH</Area>
<USED>
<type id='123' />
<type id='345' />
</USED>
<DEMAND>
<type id='809' />
<type id='321' />
</DEMAND>
<CLOSED>
<type id='456' />
<type id='765' />
</CLOSED>
</LIST>
</TOPIC>
Here i want to print only the id
under <DEMAND>
. i have tried the below code.
from xml.dom import minidom
root=minidom.parse('sample.xml')
tag=root.getElementsByTagName('type')
for i in tag:
print(i.getAttribute("id"))
But this is printing all the id
values like below.
123
345
809
321
456
765
How can i get only 809
& 321
that are under <DEMAND>
tag. I can give path in ElementTree
but not sure how to give in getElementsByTagName
? Is it even possible in Minidom?
for demand in root.getElementsByTagName('DEMAND'):
for tp in demand.getElementsByTagName('type'):
print(tp.getAttribute("id"))