Reputation: 1
I have an xml file with the root node. Under the root I have another node and under it another node.
I want to get all of the attributes and values of both the subtag and its subtag. I want to do it by using the Menu name. For example, find the attributes of the menu File and all of its subtags and their attributes.
My xml file is of this form:
<root>
<Menu name="File" text="File" toolTip="File" hide="yes">
<Action name="file2"/>
<Action name="file3"/>
</Action>
</Menu>
</root>
I basically need all of this:
<Menu name="File" text="File" toolTip="File" hide="yes">
<Action name="file2"/>
<Action name="file3"/>
</Action>
</Menu>
How can i do this using python?
Thanks.
Upvotes: 0
Views: 489
Reputation: 101
You can use the xml.etree.ElementTree module. Documentation: https://docs.python.org/3/library/xml.etree.elementtree.html#module-xml.etree.ElementTree
Here's an example:
import xml.etree.ElementTree as ET
# parse a .xml file
tree = ET.parse('test.xml')
# get the root tag from the xml
root = tree.getroot()
# for each child tag of 'Menu' prints attributes names and values
for child in root.find('Menu'):
print(child.items())
Upvotes: 1