Home > Software engineering >  How to start parsing arxml file from in python?
How to start parsing arxml file from in python?

Time:08-18

<ECUC-CONTAINER-VALUE>
                   <SHORT-NAME>WhlDist_Prtctd_PDU_PA_CAN_1_controller</SHORT-NAME>
                   <DEFINITION-REF DEST="ECUC-PARAM-CONF-CONTAINER-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu</DEFINITION-REF>
                   <PARAMETER-VALUES>
                    <ECUC-TEXTUAL-PARAM-VALUE>
                     <DEFINITION-REF DEST="ECUC-ENUMERATION-PARAM-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduDirection</DEFINITION-REF>
                     <VALUE>RECEIVE</VALUE>
                    </ECUC-TEXTUAL-PARAM-VALUE>
                    <ECUC-TEXTUAL-PARAM-VALUE>
                     <DEFINITION-REF DEST="ECUC-ENUMERATION-PARAM-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduSignalProcessing</DEFINITION-REF>
                     <VALUE>IMMEDIATE</VALUE>
                    </ECUC-TEXTUAL-PARAM-VALUE>
                    <ECUC-TEXTUAL-PARAM-VALUE>
                     <DEFINITION-REF DEST="ECUC-ENUMERATION-PARAM-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduType</DEFINITION-REF>
                     <VALUE>NORMAL</VALUE>
                    </ECUC-TEXTUAL-PARAM-VALUE>
                   </PARAMETER-VALUES>
                   <REFERENCE-VALUES>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduGroupRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/Com/ComConfig/PN_IngressEgress_PA_CAN_1_IN</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduGroupRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/Com/ComConfig/PN_PostCollisionOperation_PA_CAN_1_IN</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduGroupRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/Com/ComConfig/PN_Propulsion_PA_CAN_1_IN</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduGroupRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/Com/ComConfig/PN_RemoteDataService_PA_CAN_1_IN</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduSignalGroupRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/Com/ComConfig/WhlDist_Prtctd</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComPduIdRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/EcuC/EcucConfigSet/PduCollection/WhlDist_Prtctd_PDU_PA_CAN_1_controller_PduR2Com</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                   </REFERENCE-VALUES>
                  </ECUC-CONTAINER-VALUE>

I have tried below code also but I did not get the desired output :

from bs4 import BeautifulSoup as Soup

def diff_method():
    handler = open('Com_Comfort_EcucValues.arxml').read()
    soup = Soup(handler,"html.parser")  
    for ecuc_container in soup.find_all('ecuc-container-value'):
        for def_ref in ecuc_container.find_all('definition-ref'):
            print(def_ref.get_text())
            if def_ref.get_text(strip=True) == '/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu':
                    print(def_ref.find_next('value').text)
            

if __name__ == "__main__":

    diff_method()

my output:

RECEIVE
IMMEDIATE
IMMEDIATE 

Expected Output:

WhlDist_Prtctd_PDU_PA_CAN_1_controller
DEST="ECUC-PARAM-CONF-CONTAINER-DEF"
DEST="ECUC-ENUMERATION-PARAM-DEF"
RECEIVE
DEST="ECUC-ENUMERATION-PARAM-DEF
IMMEDIATE
DEST="ECUC-ENUMERATION-PARAM-DEF"
NORMAL

I am working on autosar files, In Autosar we are using .arxml files, In below arxml file I want parse some data. using Beautifull Soup I wanted to grap the value as mention in expected output.

CodePudding user response:

Your script do not provide the described my output, so question needs some improvement.

Assuming you only want to get the <VALUE> texts you could deal with:

for e in soup.select('ecuc-container-value [DEST="ECUC-ENUMERATION-PARAM-DEF"] value'):
    print(e.text)

Example

from bs4 import BeautifulSoup

xml='''
<ECUC-CONTAINER-VALUE>
                   <SHORT-NAME>WhlDist_Prtctd_PDU_PA_CAN_1_controller</SHORT-NAME>
                   <DEFINITION-REF DEST="ECUC-PARAM-CONF-CONTAINER-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu</DEFINITION-REF>
                   <PARAMETER-VALUES>
                    <ECUC-TEXTUAL-PARAM-VALUE>
                     <DEFINITION-REF DEST="ECUC-ENUMERATION-PARAM-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduDirection</DEFINITION-REF>
                     <VALUE>RECEIVE</VALUE>
                    </ECUC-TEXTUAL-PARAM-VALUE>
                    <ECUC-TEXTUAL-PARAM-VALUE>
                     <DEFINITION-REF DEST="ECUC-ENUMERATION-PARAM-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduSignalProcessing</DEFINITION-REF>
                     <VALUE>IMMEDIATE</VALUE>
                    </ECUC-TEXTUAL-PARAM-VALUE>
                    <ECUC-TEXTUAL-PARAM-VALUE>
                     <DEFINITION-REF DEST="ECUC-ENUMERATION-PARAM-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduType</DEFINITION-REF>
                     <VALUE>NORMAL</VALUE>
                    </ECUC-TEXTUAL-PARAM-VALUE>
                   </PARAMETER-VALUES>
                   <REFERENCE-VALUES>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduGroupRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/Com/ComConfig/PN_IngressEgress_PA_CAN_1_IN</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduGroupRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/Com/ComConfig/PN_PostCollisionOperation_PA_CAN_1_IN</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduGroupRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/Com/ComConfig/PN_Propulsion_PA_CAN_1_IN</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduGroupRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/Com/ComConfig/PN_RemoteDataService_PA_CAN_1_IN</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduSignalGroupRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/Com/ComConfig/WhlDist_Prtctd</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComPduIdRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/EcuC/EcucConfigSet/PduCollection/WhlDist_Prtctd_PDU_PA_CAN_1_controller_PduR2Com</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                   </REFERENCE-VALUES>
                  </ECUC-CONTAINER-VALUE>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(xml)

for e in soup.select('ecuc-container-value [DEST="ECUC-ENUMERATION-PARAM-DEF"] value'):
    print(e.text)

Output

RECEIVE
IMMEDIATE
NORMAL

CodePudding user response:

This is one way to get the expected output, using lxml:

from lxml import etree

string = '''
<ECUC-CONTAINER-VALUE>
                   <SHORT-NAME>WhlDist_Prtctd_PDU_PA_CAN_1_controller</SHORT-NAME>
                   <DEFINITION-REF DEST="ECUC-PARAM-CONF-CONTAINER-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu</DEFINITION-REF>
                   <PARAMETER-VALUES>
                    <ECUC-TEXTUAL-PARAM-VALUE>
                     <DEFINITION-REF DEST="ECUC-ENUMERATION-PARAM-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduDirection</DEFINITION-REF>
                     <VALUE>RECEIVE</VALUE>
                    </ECUC-TEXTUAL-PARAM-VALUE>
                    <ECUC-TEXTUAL-PARAM-VALUE>
                     <DEFINITION-REF DEST="ECUC-ENUMERATION-PARAM-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduSignalProcessing</DEFINITION-REF>
                     <VALUE>IMMEDIATE</VALUE>
                    </ECUC-TEXTUAL-PARAM-VALUE>
                    <ECUC-TEXTUAL-PARAM-VALUE>
                     <DEFINITION-REF DEST="ECUC-ENUMERATION-PARAM-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduType</DEFINITION-REF>
                     <VALUE>NORMAL</VALUE>
                    </ECUC-TEXTUAL-PARAM-VALUE>
                   </PARAMETER-VALUES>
                   <REFERENCE-VALUES>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduGroupRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/Com/ComConfig/PN_IngressEgress_PA_CAN_1_IN</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduGroupRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/Com/ComConfig/PN_PostCollisionOperation_PA_CAN_1_IN</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduGroupRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/Com/ComConfig/PN_Propulsion_PA_CAN_1_IN</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduGroupRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/Com/ComConfig/PN_RemoteDataService_PA_CAN_1_IN</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComIPduSignalGroupRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/Com/ComConfig/WhlDist_Prtctd</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                    <ECUC-REFERENCE-VALUE>
                     <DEFINITION-REF DEST="ECUC-REFERENCE-DEF">/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu/ComPduIdRef</DEFINITION-REF>
                     <VALUE-REF DEST="ECUC-CONTAINER-VALUE">/RB/UBK/Project/EcucModuleConfigurationValuess/EcuC/EcucConfigSet/PduCollection/WhlDist_Prtctd_PDU_PA_CAN_1_controller_PduR2Com</VALUE-REF>
                    </ECUC-REFERENCE-VALUE>
                   </REFERENCE-VALUES>
                  </ECUC-CONTAINER-VALUE>

'''
root = etree.fromstring(string)

for element in root.iter(): 
    if element.text == "/AUTOSAR_Com/EcucModuleDefs/Com/ComConfig/ComIPdu":
        print(element.getprevious().text)
        print(element.attrib)
        for c in element.getnext().iter():
            if c.tag == 'DEFINITION-REF':
                print(c.attrib)
            elif c.tag == 'VALUE':
                print(c.text)

This will print in terminal:

WhlDist_Prtctd_PDU_PA_CAN_1_controller
{'DEST': 'ECUC-PARAM-CONF-CONTAINER-DEF'}
{'DEST': 'ECUC-ENUMERATION-PARAM-DEF'}
RECEIVE
{'DEST': 'ECUC-ENUMERATION-PARAM-DEF'}
IMMEDIATE
{'DEST': 'ECUC-ENUMERATION-PARAM-DEF'}
NORMAL
  • Related