我有一個XML和一個代碼。但我無法弄清楚如何做這個部分。使用xml屬性匹配輸入的Python
XML:
<settings>
<plugin id="1" name="Send Mail" execute="func/sendmail.py">
<param name="towhere" value="To Where?" />
<param name="mailsub" value="Mail Subject" />
<param name="mailcont" value="Mail Content" />
<param name="senderid" value="[email protected]" />
<param name="senderpw" value="xxxx" />
</plugin>
<plugin id="2" name="Open Tar" execute="func/taropen.py">
<param name="tarname" value="Tar file name" />
<param name="tarloc" value="Tar location" />
<param name="tardest" value="Tar destination" />
</plugin>
<plugin id="3" name="Server Reboot" execute="func/reboot.py">
<param name="rebootafter" value="1" />
</plugin>
</settings>
代碼:
from xml.dom import minidom
yXML = minidom.parse('data/config.xml')
for plugin in yXML.getElementsByTagName('plugin'):
print plugin.getAttribute('id')+"- "+plugin.getAttribute('name')
selection = raw_input("Enter your choice: ")
selection = plugin.getAttribute('id')
if selection:
import os
exe = plugin.getAttribute('execute')
os.system('python '+exe)
當我1型或2作爲輸入,它打開3號。你能幫忙嗎?
輸出:
ubuntu:~/Desktop$ python test.py
1- Send Mail
2- Open Tar
3- Server Reboot
Enter your choice: 1
python: can't open file 'func/reboot.py': [Errno 2] No such file or directory
ubuntu:~/Desktop$
這太容易解決,但我不認爲很好。
預先感謝您!
您可以使用BeautifulSoup for Python,它可以解析HTML和XML。解析時,可以通過指定文本「text」來指定要搜索的文本。 – Griffin