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