用python和lxm解析vcxproj

2024-09-30 16:27:35 发布

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

我试图用Python和lxml解析vcxproj。当我尝试这样做时,除非我删除<Project >中的内容,否则在打印过程中不会出现任何内容。在

这是我的.vcxproj(我将其简化为测试):

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|Win32">
      <Configuration>Debug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Debug|x64">
      <Configuration>Debug</Configuration>
      <Platform>x64</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="ReleaseDebug|Win32">
      <Configuration>ReleaseDebug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="ReleaseDebug|x64">
      <Configuration>ReleaseDebug</Configuration>
      <Platform>x64</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|Win32">
      <Configuration>Release</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|x64">
      <Configuration>Release</Configuration>
      <Platform>x64</Platform>
    </ProjectConfiguration>
  </ItemGroup>
</Project>

以及我的python代码:

^{pr2}$

如果我这样运行,脚本会起作用,但不会显示任何内容。如果我在nodeProject脚本中删除DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003",脚本会起作用。。。在

我是xml新手,也许我做错了什么。你能帮我解决这个问题吗?在

谢谢你的帮助。在


Tags: debugproject脚本内容releaseincludexmlconfiguration
1条回答
网友
1楼 · 发布于 2024-09-30 16:27:35

在此处找到解决方案:lxml etree xmlparser remove unwanted namespace

似乎,我必须在名称空间之前(如果有)像这样精确:

from lxml import etree

tree = etree.parse("core.xml")

namespaces = {'ns':'http://schemas.microsoft.com/developer/msbuild/2003'}
for conf in tree.xpath('//ns:Configuration', namespaces=namespaces):
    print (conf.text)

相关问题 更多 >