繁体   English   中英

Python:xml.Find总是不返回

[英]Python: xml.Find always returns none

因此,我正在尝试使用python搜索并替换vcxproj文件中的xml关键字RunCodeAnalysis。 我是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'))

这是在线的简化代码示例: https : //ideone.com/1T1wsb

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

好的,所以@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])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM