从 Linux 中的 .XML 文件获取值

从 Linux 中的 .XML 文件获取值
  • 有谁能给这个问题一个好的解决方案:

  • /PlayDirList/PlayDir.1378803204我想要这个 XML 中的值: 。

    <settings>
    
        <setting id="ask_set_logo_to_bg" value="false" />
        <setting id="check_update_interval" value="0" />
        <setting id="clientid" value="DMDS-MVG-0001" />
        <setting id="guinotify" value="false" />
        <setting id="pil_is_broken" value="false" />
        <setting id="playdir" value="/PlayDirList/PlayDir.1378803204" />
        <setting id="runonstartup" value="true" />
        <setting id="schedule_poweroff" value="false" />
        <setting id="schedule_poweroff1" value="false" />
        <setting id="schedule_poweroff2" value="false" />
        <setting id="schedule_poweroff_custom" value="false" />
        <setting id="schedule_poweroff_from_day0" value="1" />
        <setting id="schedule_poweroff_from_day1" value="5" />
        <setting id="schedule_poweroff_from_day2" value="5" />
        <setting id="schedule_poweroff_hour0" value="23" />
        <setting id="schedule_poweroff_hour1" value="23" />
        <setting id="schedule_poweroff_hour2" value="23" />
        <setting id="schedule_poweroff_min0" value="0" />
        <setting id="schedule_poweroff_min1" value="0" />
        <setting id="schedule_poweroff_min2" value="0" />
        <setting id="schedule_poweroff_to_day0" value="0" />
        <setting id="schedule_poweroff_to_day1" value="6" />
        <setting id="schedule_poweroff_to_day2" value="0" />
        <setting id="stat_report_interval" value="600" />
        <setting id="sync_screenshot" value="true" />
        <setting id="use_external_downloader" value="true" />
        <setting id="boot_time" value="2013-10-07 09:29:23" />
    </settings>
    
  • 它是一个 Linux 机器,我不喜欢安装任何额外的应用程序来完成它。因此,我更喜欢一个好的 perl、python 或 bash 解决方案。

答案1

您可以使用xmllintXPATH 来实现:

xmllint --xpath //setting[id='playdir']/@value xmlfilename.xml

或者如果你使用 python,

import libxml2

doc = libxml2.parseFile('foo.xml')
for setting in doc.xpathEval('//setting[id="playdir"]'):
  print setting.prop('value')

答案2

gawk '/id="playdir"/{match($0,/value="[^"]*/); print substr($0, RSTART+7,RLENGTH-7)}'

相关内容