我正在尝试自动迭代思考应用程序的所有菜单。这是XML文件的一部分:
<?xml version="1.0" encoding="UTF-8"?>
<ke3600-menu-file language="en" display="English" index="1">
<version major="0" minor="1" patch="0"/>
<locale name="en_EN" timezone="CET-1CEST,M3.5.0,M10.5.0/3"/>
<menu name="main_menu" display="Main Menu">
<menu name="broadband" display="Broadband" help="100_help_broadband">
<onenter proc="activateGfast"/>
<menu name="load_save_profiles" display="Load and Save Profiles" help="601_help_profiles">
<application name="load_profiles" display="Load Profile"/>
<application name="save_profiles" display="Save Profile"/>
<application name="remove_profiles" display="Delete Profile"/>
</menu>
<parameter type="list" name="wanInterface" display="Interface" help="101_help_wanInterface">
<value id="0" name="ifDSL" display="xDSL"/>
<value id="1" name="ifSFP" display="SFP"/>
<value id="2" name="ifETH" display="Ethernet"/>
<!-- <value i d="3" name="ifWLAN" display="WLAN"/> -->
<value id="4" name="ifSHDSL" display="SHDSL"/>
</parameter>
<menu name="xdslinterface" display="xDSL Interface" help="200_help_xDslInterface">
<parameter type="list" name="annex" display="xDSL Mode" help="201_help_xdslModi">
<value id="0" name="xdsl_line_annex_a" display="Annex A/M"/>
<value id="1" name="xdsl_line_annex_b" display="Annex B/J"/>
</parameter>
<application name="xdsl_macAddr" display="MAC Address" caption="MAC Address">
<param type="string" name="xdsl_mac_addr" display="MAC Address" length="25"/>
</application>
<parameter type="list" name="xdsl_vectoring_mode" display="Vectoring Mode" help="202_help_Vectoring">
<value id="0" name="xdsl_vectoring_mode_disabled" display="Disabled"/>
<value id="1" name="xdsl_vectoring_mode_enabled" display="Enabled"/>
<value id="2" name="xdsl_vectoring_mode_friendly" display="Friendly"/>
</parameter>
<parameter type="list" name="xdsl_gfast_mode" display="G.FAST">
<value id="0" name="xdsl_gfast_mode_disabled" display="Disabled"/>
<value id="1" name="xdsl_gfast_mode_enabled" display="Enabled"/>
</parameter>
<parameter type="list" name="resync" display="Auto Re-Sync">
<value id="1" name="re_sync_on" display="On"/>
<value id="0" name="re_sync_off" display="Off"/>
</parameter>
<parameter type="list" name="seamlessRateAdaption" display="Seamless Rate Adaption">
<value id="0" name="sra_off" display="Off"/>
<value id="1" name="sra_on" display="On"/>
</parameter>
<parameter type="list" name="xdsl_ginp" display="G.INP">
<value id="0" name="xdsl_ginp_none" display="Off"/>
<value id="1" name="xdsl_ginp_down" display="Down"/>
<value id="2" name="xdsl_ginp_up" display="Up"/>
<value id="3" name="xdsl_ginp_both" display="Down/Up"/>
</parameter>
<parameter type="list" name="power_cut_back" display="Power Cut Back">
<value id="0" name="power_cut_back_off" display="Off"/>
<value id="1" name="power_cut_back_on" display="On"/>
</parameter>
<parameter type="list" name="atmType" display="ATM Type">
<value id="0" name="atm_type_aal5" display="AAL5"/>
<value id="1" name="atm_type_aal0pkt" display="AAL0PKT"/>
<value id="2" name="atm_type_aal0cell" display="AAL0CELL"/>
</parameter>
<parameter type="list" name="voip_atmType" display="Voip Interface ATM Type">
<value id="0" name="voip_atm_type_aal5" display="AAL5"/>
<value id="1" name="voip_atm_type_aal0pkt" display="AAL0PKT"/>
<value id="2" name="voip_atm_type_aal0cell" display="AAL0CELL"/>
</parameter>
<parameter type="list" name="iptv_atmType" display="Iptv Interface ATM Type">
<value id="0" name="iptv_atm_type_aal5" display="AAL5"/>
<value id="1" name="iptv_atm_type_aal0pkt" display="AAL0PKT"/>
<value id="2" name="iptv_atm_type_aal0cell" display="AAL0CELL"/>
</parameter>
</menu>
<menu name="ethernetinterface" display="Ethernet Interface" help="300_help_ethernetInterface">
<parameter type="list" name="ethInterfaceAuto" display="Autonegotiation">
<value id="1" name="ethInterfaceAutoOn" display="On"/>
<value id="0" name="ethInterfaceAutoOff" display="Off"/>
</parameter>
<parameter type="list" name="ethInterfaceSpeed" display="Speed">
<value id="2" name="ethIf1000" display="1000MBit/s"/>
<value id="1" name="ethIf100" display="100MBit/s"/>
<value id="0" name="ethIf10" display="10MBit/s"/>
</parameter>
<parameter type="list" name="ethInterfaceDuplexMode" display="Duplex Mode">
<value id="1" name="ethIfFull" display="Full"/>
<value id="0" name="ethIfHalf" display="Half"/>
</parameter>
</menu>
我想创建一个循环,它将使用深度优先搜索(遍历从根开始的树并在回溯之前尽可能沿着每个分支探索)方法遍历所有菜单。到目前为止,我可以获得特定节点的价值,但我无法弄清楚如何自动浏览所有menu
,submenu
,application
,parameter
,value
并打印其display
值。以下是获取特定分支的display
值的代码。
import xml.etree.ElementTree as ET
def main():
tree = ET.parse("/home/mr/Desktop/git/KE3600_APP/menu.xml")
result = ''
root = tree.getroot()
for e in tree.findall('.//'):
test.log(str(root.attrib.get('display')))
我已经查看了各种教程,但是所有教程都只是去了一个特定的节点并提取了值,它们都没有通过所有这些。请建议我帮助我解决这个问题的任何教程或任何示例解决方案。
答案 0 :(得分:1)
您可以从root进行迭代:
import xml.etree.ElementTree as ET
xml_file = "menus.xml"
tree = ET.parse(xml_file)
root = tree.getroot()
def process_menu(menu):
print('Processing: {}'.format(menu.get('display')))
for item in menu.getchildren():
if (item.tag != "menu"):
print(item)
else:
process_menu(item)
for menu in root.iter("menu"):
process_menu(menu)