【问题标题】:Print all xml child node using python使用python打印所有xml子节点
【发布时间】:2017-07-30 16:18:21
【问题描述】:

我想打印我的 xml 文件的“ItemGroup”的“ClCompiler”子项的所有值。

我的python代码

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

我的结果

<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


我的 xml 的一部分

<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>

【问题讨论】:

    标签: python xml-parsing elementtree minidom


    【解决方案1】:

    您成功了。
    您在文档中找到了所有 ItemGroup 节点。现在,您必须遍历它们中的每一个并找到它的 ClCompile 子级(很可能只有其中一个会有这样的子级)。

    代码如下:

    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()
    

    注意事项

    • 我使用 Python 3.4 运行代码(因为问题中没有提到版本)。 2.7 兼容性需要一些小的改动。
    • 我在一个 VStudio 项目上进行了测试,其中第二个搜索标签是 ClInclude,但我猜这是一个相当旧的版本。
    • 第 1stprint 行仅用于说明父 ItemGroup 节点。将其注释掉以实现您想要的输出。
    • 不用说您应该修改project_path 以指向您的项目文件。

    【讨论】:

      【解决方案2】:

      使用 ElementTree 的替代解决方案,

      import xml.etree.ElementTree as ET
      root = ET.fromstring('''\
      <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>
      ''')
      
      for child in root.iter('ClCompile'):
          print ET.tostring(child)
      

      从文件中解析时,

      import xml.etree.ElementTree as ET
      tree=ET.parse('text.xml')
      root = tree.getroot()
      for child in root.iter('ClCompile'):
          print ET.tostring(child)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-11
        • 1970-01-01
        相关资源
        最近更新 更多