使用python打印所有xml子节点 [英] Print all xml child node using python

查看:343
本文介绍了使用python打印所有xml子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打印我的xml文件的 ItemGroup子项的 ClCompiler子项的所有值。

I want to print all the values of the "ClCompiler" child of "ItemGroup" of my xml file.

tree = minidom.parse(project_path)
itemgroup = tree.getElementsByTagName('ItemGroup')
print (itemgroup[0].toxml())



我的结果



my result

<ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|Win32">
        <Configuration>Debug</Configuration>
        <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|Win32">
        <Configuration>Release</Configuration>
        <Platform>Win32</Platform>
    </ProjectConfiguration>
</ItemGroup>
<ItemGroup>
    <ClCompile Include="../../avmedia/source/framework/MediaControlBase.cxx"/>
    <ClCompile Include="../../avmedia/source/framework/mediacontrol.cxx"/>
    <ClCompile Include="../../avmedia/source/framework/mediaitem.cxx"/>
    <ClCompile Include="../../avmedia/source/framework/mediamisc.cxx"/>
</ItemGroup>

ecc

    <ClCompile Include="../../basic/source/basmgr/basmgr.cxx"/>         
    <ClCompile Include="../../basic/source/basmgr/vbahelper.cxx"/>      
    <ClCompile Include="../../basic/source/classes/codecompletecache.cxx"/>

ecc

<ItemGroup>
    <ClCompile Include="../../basic/source/basmgr/basicmanagerrepository.cxx"/>
    <ClCompile Include="../../basic/source/basmgr/basmgr.cxx"/>
    <ClCompile Include="../../basic/source/basmgr/vbahelper.cxx"/>
    <ClCompile Include="../../basic/source/classes/codecompletecache.cxx"/>
</ItemGroup>


推荐答案

您成功了一半。
您找到了文档中的所有 ItemGroup 节点。现在,您必须遍历每个对象并找到其 ClCompile 子对象(很可能只有一个子对象具有此类孩子)。

You made it half way.
You found all the ItemGroup nodes in the document. Now, you have to iterate through each of them and find its ClCompile children (most likely only one of them will have such children).

这是代码:

from xml.dom import minidom

project_path = "./a.vcxproj"
item_group_tag = "ItemGroup"
cl_compile_tag = "ClCompile"


def main():
    tree = minidom.parse(project_path)
    item_group_nodes = tree.getElementsByTagName(item_group_tag)
    for idx, item_group_node in enumerate(item_group_nodes):
        print("{} {} ------------------".format(item_group_tag, idx))
        cl_compile_nodes = item_group_node.getElementsByTagName(cl_compile_tag)
        for cl_compile_node in cl_compile_nodes:
            print("\t{}".format(cl_compile_node.toxml()))


if __name__ == "__main__":
    main()

注释


  • 我用运行了代码> P ython 3.4 (因为该问题中未提及任何版本)。 2.7 兼容性需要进行一些小的更改。

  • 我在第二个搜索标签为 VStudio 项目上进行了测试ClInclude ,但我想那是一个相当老的版本。

  • 第一个 st print 线仅用于说明父 ItemGroup 节点。注释掉它以达到所需的输出。

  • 无需多说,您应该修改 project_path 指向您的项目文件。
  • li>
  • I ran the code with Python 3.4 (since no version was mentioned in the question). 2.7 compatibility will require some minor changes.
  • I did my tests on a VStudio project where the second search tag was ClInclude, but I guess that's an fairly old version.
  • The 1st print line is only for illustrating the parent ItemGroup node. Comment it out to achieve your desired output.
  • Needless to say that you should modify project_path to point to your project file.

这篇关于使用python打印所有xml子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆