如何根据父标记中的给定属性获取文件名和路径

2024-09-19 23:41:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想更改下面的代码以获得file_namesfile_paths只有当父标记中存在fastboot="true"属性时,我提供了当前输出和预期输出,有人能提供如何操作的指导吗?你知道吗

import sys
import os
import string
from xml.dom import minidom

if __name__ == '__main__':
    meta_contents = minidom.parse("fast.xml")
    builds_flat = meta_contents.getElementsByTagName("builds_flat")[0]
    build_nodes = builds_flat.getElementsByTagName("build")
    for build in build_nodes:
          bid_name = build.getElementsByTagName("name")[0]
          print "Checking if this is cnss related image... : \n"+bid_name.firstChild.data
          if (bid_name.firstChild.data == 'apps'):
             file_names = build.getElementsByTagName("file_name")
             file_paths = build.getElementsByTagName("file_path")
             print "now files paths...\n"
             for fn,fp in zip(file_names,file_paths):
                if (not fp.firstChild.nodeValue.endswith('/')):
                   fp.firstChild.nodeValue = fp.firstChild.nodeValue + '/'
                full_path = fp.firstChild.nodeValue+fn.firstChild.nodeValue
                print "file-to-copy: "+full_path
             break

输入XML:-你知道吗

  <builds_flat>
    <build>
      <name>apps</name>
      <file_ref ignore="true" minimized="true">
        <file_name>adb.exe</file_name>
        <file_path>LINUX/android/vendor/qcom/proprietary/usb/host/windows/prebuilt/</file_path>
      </file_ref>
      <file_ref ignore="true" minimized="true">
        <file_name>system.img</file_name>
        <file_path>LINUX/android/out/target/product/msmcobalt/secondary-boot/</file_path>
      </file_ref>
      <download_file cmm_file_var="APPS_BINARY" fastboot_rumi="boot" fastboot="true" minimized="true">
        <file_name>boot.img</file_name>
        <file_path>LINUX/android/out/target/product/msmcobalt/</file_path>
      </download_file>
      <download_file sparse_image_path="true" fastboot_rumi="abl" fastboot="true" minimized="true">
        <file_name>abl.elf</file_name>
        <file_path>LINUX/android/out/target/product/msmcobalt/</file_path>
      </download_file>      
    </build>
  </builds_flat>

你知道吗输出:-你知道吗

...............
now files paths...

file-to-copy: LINUX/android/vendor/qcom/proprietary/usb/host/windows/prebuilt/adb.exe
file-to-copy: LINUX/android/out/target/product/msmcobalt/secondary-boot/system.img
file-to-copy: LINUX/android/out/target/product/msmcobalt/boot.img
file-to-copy: LINUX/android/out/target/product/msmcobalt/abl.elf

预期输出:-你知道吗

now files paths...

........
file-to-copy: LINUX/android/out/target/product/msmcobalt/boot.img
file-to-copy: LINUX/android/out/target/product/msmcobalt/abl.elf

Tags: topathnamebuildtruetargetlinuxproduct
1条回答
网友
1楼 · 发布于 2024-09-19 23:41:27

我想到了一个相当迅速和肮脏的事实,即只有download_file元素才有fastboot属性,对吧?如果是这种情况,您可以始终获取download_file类型的子级,并筛选其fastboot属性不是"true"的子级:

import os
from xml.dom import minidom

if __name__ == '__main__':
    meta_contents = minidom.parse("fast.xml")
    for elem in meta_contents.getElementsByTagName('download_file'):
        if elem.getAttribute('fastboot') == "true":
            path = elem.getElementsByTagName('file_path')[0].firstChild.nodeValue
            file_name = elem.getElementsByTagName('file_name')[0].firstChild.nodeValue
            print os.path.join(path, file_name)

您提供的示例输出:

$ python ./stack_034.py
LINUX/android/out/target/product/msmcobalt/boot.img
LINUX/android/out/target/product/msmcobalt/abl.elf

不用说。。。由于没有.xsd文件(当然,这与minidom也没有关系),所以只会得到字符串(没有类型安全性),这只适用于示例中所示的结构(我的意思是,您可能希望在这里添加一些额外的检查)

编辑: 根据这份答复中的评论:

要获取<build>中包含值为apps<name>属性的元素,可以:找到<name>标记(值为字符串apps的标记),然后移动到父节点(该节点将把您放入build元素中),然后按上述步骤进行:

if __name__ == '__main__':
    meta_contents = minidom.parse("fast.xml")
    for elem in meta_contents.getElementsByTagName('name'):
        if elem.firstChild.nodeValue == "apps":
            apps_build = elem.parentNode
            for elem in apps_build.getElementsByTagName('download_file'):
                if elem.getAttribute('fastboot') == "true":
                    path = elem.getElementsByTagName('file_path')[0].firstChild.nodeValue
                    file_name = elem.getElementsByTagName('file_name')[0].firstChild.nodeValue
                    print os.path.join(path, file_name)

相关问题 更多 >