'How to get data inside a Tag line in Xml using python?

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated by ABREX Ver. 1.1.0 -->
<AUTOSAR xmlns="http://autosar.org/schema/r4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://autosar.org/schema/r4.0 AUTOSAR_4-0-3_STRICT.xsd">
    <AR-PACKAGES>
        <AR-PACKAGE>
            <SHORT-NAME>Ecuc</SHORT-NAME>
            <ELEMENTS>
                <ECUC-MODULE-CONFIGURATION-VALUES>
                    <SHORT-NAME>Os</SHORT-NAME>
                    <DEFINITION-REF DEST="ECUC-MODULE-DEF">/AUTOSAR/EcucDefs/Os</DEFINITION-REF>
                    <ECUC-DEF-EDITION>4.2.0</ECUC-DEF-EDITION>
                    <IMPLEMENTATION-CONFIG-VARIANT>VARIANT-PRE-COMPILE</IMPLEMENTATION-CONFIG-VARIANT>
                    <CONTAINERS>
                        <ECUC-CONTAINER-VALUE>
                            <SHORT-NAME>OsInclude</SHORT-NAME>
                            <DEFINITION-REF DEST="ECUC-PARAM-CONF-CONTAINER-DEF">/AUTOSAR/EcucDefs/Os/OsInclude</DEFINITION-REF>
                            <PARAMETER-VALUES>
                                <ECUC-TEXTUAL-PARAM-VALUE>
                                    <DEFINITION-REF DEST="ECUC-STRING-PARAM-DEF">/AUTOSAR/EcucDefs/Os/OsInclude/OsIncludeFileName</DEFINITION-REF>
                                    <VALUE>sample1.h</VALUE>
                                </ECUC-TEXTUAL-PARAM-VALUE>
                            </PARAMETER-VALUES>
                        </ECUC-CONTAINER-VALUE>
                        <ECUC-CONTAINER-VALUE>
                            <SHORT-NAME>AppMode1</SHORT-NAME>
                            <DEFINITION-REF DEST="ECUC-PARAM-CONF-CONTAINER-DEF">/AUTOSAR/EcucDefs/Os/OsAppMode</DEFINITION-REF>
                        </ECUC-CONTAINER-VALUE>                     
                </ECUC-MODULE-CONFIGURATION-VALUES>
            </ELEMENTS>
        </AR-PACKAGE>
    </AR-PACKAGES>
</AUTOSAR>

This is my code. I want to find the value of the Tag from "DEFINITION-REF", which is 'DEST'='ECUC-ENUMERATION-PARAM-DEF'. But I am not able to get the value of 'DEST'. when I run the below code, it is always 'None'. Can you provide any solutions?

Code:

    import xml.etree.ElementTree as ET
    import csv
    fpath = 'sample1.arxml'

    tree = ET.parse(fpath)
    root = tree.getroot()

    ns = {'ns': 'http://autosar.org/schema/r4.0'}
    for param1 in root.findall('.//ns:DEFINITION-REF/',  namespaces=ns):
        value = param1.get('DEST')
        print(value)


Solution 1:[1]

I used the file you shared and was able to get the attributes for DEFINITION-REF :

import xml.etree.ElementTree as ET

root = ET.parse('test.xml').getroot()


#run this, and it will list the key value pairs for DEFINITION-REF element : 
for entry in root.findall(".//{http://autosar.org/schema/r4.0}DEFINITION-REF"):
    print(entry.attrib)

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 halfer