Python:xml.查找总是返回非

2024-09-30 22:20:41 发布

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

因此,我尝试用python搜索并替换vcxproj文件中的xml关键字runcodealysis。 我对python还很陌生,所以要温柔一点,但我认为这是最简单的语言来做这种事情。 我阅读了一些类似的示例,并得出了下面的代码,但是无论我搜索ElementTree Find调用,总是返回None。在

from xml.etree import ElementTree as et

xml = '''\
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Protected_Debug|Win32'">
      <RunCodeAnalysis>false</RunCodeAnalysis>
   </PropertyGroup>
</Project>
'''

et.register_namespace('', "http://schemas.microsoft.com/developer/msbuild/2003")
tree = et.ElementTree(et.fromstring(xml))
print(tree.find('.//RunCodeAnalysis'))

{a在线示例:^ a1的简化代码

谁能告诉我我做错了什么吗?在


Tags: 代码projectcomtreehttp示例developerxml
1条回答
网友
1楼 · 发布于 2024-09-30 22:20:41

好吧。。所以@ThomWiggers帮我解决了缺失的部分,这是我的最后一段代码。还没有参数检查或任何类型的智能,但它需要两个参数-文件名和是否将静态代码分析转换为true或false。我有大约30个项目,我想打开它为夜间建设,但真的不想打开它,因为它只是太慢了。在

import sys
from xml.etree import ElementTree as et

et.register_namespace('', "http://schemas.microsoft.com/developer/msbuild/2003")
tree = et.parse(sys.argv[1])
value = sys.argv[2]
for item in tree.findall('.//{http://schemas.microsoft.com/developer/msbuild/2003}RunCodeAnalysis'):
    item.text = value
for item in tree.findall('.//{http://schemas.microsoft.com/developer/msbuild/2003}EnablePREfast'):
    item.text = value
tree.write(sys.argv[1])

相关问题 更多 >