'How to access the tag below another tag in xml using xml.dom.minidom in python?

I am using python 3.10.4 . I am new at parsing xml files. like for eg, let the xml file be with the filename "test.xml":

<?xml version="1.0" encoding="UTF-8"?>
<tag1 name="1">
        <tag2 name="a"></tag2>
        </tag1>
        <tag1 name = "2">
        <tag2 name = "b"></tag2>
        </tag1>
        </xml>

python code

import xml.dom.minidom
file = xml.dom.minidom.parse('test.xml')
list = []
tags=file.getElementsByTagName("tag1")
for tag in tags:
if(tag.getAttribute("name")=="1"):
print(tag.getAttribute("tag2"))

So here I want to access the tag2 of tag1 with name="1". How can I do it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source