I'm new to python and I have an xml file which looks like this:
<PARAMETERS>
<PARAMETER name="parameter_1" type="INTEGER" unit="m" description="parameter_1 description" defaultValue="10"> <RUBRIC name="ENG"/> </PARAMETER>
<PARAMETER name="parameter_2" type="REAL" unit="ft" description="parameter_2 description" defaultValue="5"> <RUBRIC name="ENG"/> </PARAMETER>
.
.
.
</PARAMETERS>
I would like to get the different attributes value from each tag "PARAMETER". I have tried to use etree from lxml but I didn't find any information about attributes.
I think this is what you want. Find the PARAMETER
elements and put them in required_elements
. Then interrogate each of the for all of it attributes in a list comprehension.
>>> from lxml import etree
>>> tree = etree.parse('temp.xml')
>>> required_elements = tree.xpath('.//PARAMETER')
>>> for element in required_elements:
... [element.attrib[_] for _ in ['name', 'type', 'unit', 'description', 'defaultValue']]
...
['parameter_1', 'INTEGER', 'm', 'parameter_1 description', '10']
['parameter_2', 'REAL', 'ft', 'parameter_2 description', '5']