Skip to main content
 首页 » 编程设计

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

2025年04月02日8linjiqin

我想更改以下代码以仅在父标记中存在 fastboot="true" 属性时获取 file_names 和 file_paths ,我提供了当前输出和预期输出,任何人都可以指导如何做吗?

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 

请您参考如下方法:

想到的一些相当快速和肮脏的事情是使用这样一个事实,即只有 download_file元素有 fastboot属性对吧?如果是这样的话,你总能得到类型为 download_file 的 child 。并过滤那些 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>内的元素包含 <name>值为 apps 的属性,您可以:找到 <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)