使用漂亮的汤或python中的任何其他方法解析Autosar arxml

问题描述 投票:0回答:1

我正在处理autosar文件,在Autosar中,我们使用的是.arxml文件,在下面的arxml文件中,我想解析一些数据(DTC值,例如:112068)

。arxml:

  <ECUC-CONTAINER-VALUE>
   <SHORT-NAME>DTC_AD</SHORT-NAME>
   <DEFINITION-REF DEST="ECUC-PARAM-CONF-CONTAINER-DEF">/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemDTCClass</DEFINITION-REF>
   <PARAMETER-VALUES>
    <ECUC-NUMERICAL-PARAM-VALUE>
     <DEFINITION-REF DEST="ECUC-INTEGER-PARAM-DEF">/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemDTCClass/DemDTC</DEFINITION-REF>
     <VALUE>112068</VALUE>
    </ECUC-NUMERICAL-PARAM-VALUE>
    <ECUC-TEXTUAL-PARAM-VALUE>
     <DEFINITION-REF DEST="ECUC-STRING-PARAM-DEF">/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemDTCClass/DemDTCDescription</DEFINITION-REF>
     <VALUE>AD temp</VALUE>
    </ECUC-TEXTUAL-PARAM-VALUE>
    <ECUC-NUMERICAL-PARAM-VALUE>
     <DEFINITION-REF DEST="ECUC-INTEGER-PARAM-DEF">/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemDTCClass/DemDTCFunctionalUnit</DEFINITION-REF>
     <VALUE>1</VALUE>
    </ECUC-NUMERICAL-PARAM-VALUE>
   </PARAMETER-VALUES>
  </ECUC-CONTAINER-VALUE>
  <ECUC-CONTAINER-VALUE>
   <SHORT-NAME>DTC_Lost</SHORT-NAME>
   <DEFINITION-REF DEST="ECUC-PARAM-CONF-CONTAINER-DEF">/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemDTCClass</DEFINITION-REF>
   <PARAMETER-VALUES>
    <ECUC-NUMERICAL-PARAM-VALUE>
     <DEFINITION-REF DEST="ECUC-INTEGER-PARAM-DEF">/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemDTCClass/DemDTC</DEFINITION-REF>
     <VALUE>126630</VALUE>
    </ECUC-NUMERICAL-PARAM-VALUE>
    <ECUC-TEXTUAL-PARAM-VALUE>
     <DEFINITION-REF DEST="ECUC-STRING-PARAM-DEF">/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemDTCClass/DemDTCDescription</DEFINITION-REF>
     <VALUE>LostCOMM</VALUE>
    </ECUC-TEXTUAL-PARAM-VALUE>
    <ECUC-NUMERICAL-PARAM-VALUE>
     <DEFINITION-REF DEST="ECUC-INTEGER-PARAM-DEF">/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemDTCClass/DemDTCFunctionalUnit</DEFINITION-REF>
     <VALUE>1</VALUE>
    </ECUC-NUMERICAL-PARAM-VALUE>
   </PARAMETER-VALUES>
  </ECUC-CONTAINER-VALUE>

我也尝试过下面的代码,但没有得到想要的输出:

from bs4 import BeautifulSoup as Soup

def diff_method():
    handler = open('Dem_PRJ_8CH_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() == '/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemDTCClass/DemDTC'):
                print(ecuc_container.get_text())

if __name__ == "__main__":

    diff_method()

预期输出:

112068
126630
python beautifulsoup xml-parsing html-parsing
1个回答
0
投票

如果handler中的变量是问题的XML文本,则可以使用此示例从<value>标记中获取值:

© www.soinside.com 2019 - 2024. All rights reserved.