从XML标记python中删除文本值

从XML标记python中删除文本值,python,Python,我只想使用python dom或elementtree删除xml文件的文本值 <?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemDefinitionGroup Condition="'$(Configur

我只想使用python dom或elementtree删除xml文件的文本值

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <PostBuildEvent>
      <Command>sign "Loc" </Command>
     </PostBuildEvent>
 </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <PostBuildEvent>
     <Command>COPY "SourceLoc" "DestLoc"</Command>
     </PostBuildEvent>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <PostBuildEvent>
      <Command>COPY "SourceLoc" "DestLoc"</Command>
     </PostBuildEvent>
 </ItemDefinitionGroup>
</Project>

签署“Loc”
复制“SourceLoc”“DestLoc”
复制“SourceLoc”“DestLoc”
我想要的是清空包含复制进程的Command标记

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

 <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <PostBuildEvent>
      <Command>sign "Loc" </Command>
     </PostBuildEvent>
 </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <PostBuildEvent>
     <Command></Command>
     </PostBuildEvent>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <PostBuildEvent>
      <Command></Command>
     </PostBuildEvent>
 </ItemDefinitionGroup>
</Project>

签署“Loc”

使用elementtree,尝试以下内容:

for tag in tree.findAll("Command"):
    tag.text = None

我只是扩展从stackoverflow得到的解决方案; 我经常使用它。。。 只需在“\uuu main”函数中定义输入和输出xmlfile名称

from xml.etree import ElementTree as ET
import re


class ElementTreeHelper():
    def __init__(self, xml_file_name):
        xml_file = open(xml_file_name, "rb")
        self.__parse_xml_declaration(xml_file)
        self.element_tree = ET.parse(xml_file)
        xml_file.seek(0)
        root_tag_namespace = self.__root_tag_namespace(self.element_tree)
        self.namespace = None
        if root_tag_namespace is not None:
            self.namespace = '{' + root_tag_namespace + '}'
            ET.register_namespace('', root_tag_namespace)
            self.element_tree = ET.parse(xml_file)

    def find(self, xpath_query):
        return self.element_tree.find(xpath_query)

    def write(self, xml_file_name):
        xml_file = open(xml_file_name, "wb")
        if self.xml_declaration_line is not None:
            xml_file.write(self.xml_declaration_line + '\n')

        return self.element_tree.write(xml_file)

    def __parse_xml_declaration(self, xml_file):
        first_line = xml_file.readline().strip()
        if first_line.startswith('<?xml') and first_line.endswith('?>'):
            self.xml_declaration_line = first_line
        else:
            self.xml_declaration_line = None
        xml_file.seek(0)

    def __root_tag_namespace(self, element_tree):
        namespace_search = re.search('^{(\S+)}', element_tree.getroot().tag)
        if namespace_search is not None:
            return namespace_search.group(1)
        else:
            return None


def __main():
    el_tree_hlp = ElementTreeHelper('myxml.xml')

    for elem in el_tree_hlp.element_tree.iter():
        if elem.text and 'COPY' in elem.text:
            elem.text=None

    el_tree_hlp.write('myxml1.xml')

if __name__ == '__main__':
    __main()
从xml.etree导入ElementTree作为ET
进口稀土
类ElementTreeHelper():
定义初始化(self,xml文件名):
xml\u file=open(xml\u file\u名称,“rb”)
self.\u解析\u xml\u声明(xml\u文件)
self.element_tree=ET.parse(xml_文件)
xml_文件.seek(0)
root\u tag\u namespace=self.\u root\u tag\u namespace(self.element\u树)
self.namespace=None
如果root_tag_名称空间不是None:
self.namespace='{'+root_tag_namespace+'}'
ET.register\u名称空间(“”,根\u标记\u名称空间)
self.element_tree=ET.parse(xml_文件)
def find(self,xpath\u查询):
返回self.element\u tree.find(xpath\u查询)
def写入(自身、xml文件名):
xml_文件=打开(xml_文件名,“wb”)
如果self.xml_声明_行不是None:
xml\u file.write(self.xml\u声明\u行+“\n”)
返回self.element\u tree.write(xml\u文件)
定义解析xml声明(self,xml文件):
第一行=xml\u file.readline().strip()
如果第一行以(“”)开头:
self.xml\u声明\u行=第一行
其他:
self.xml\u声明\u行=无
xml_文件.seek(0)
定义根标记命名空间(自、元素树):
名称空间_search=re.search('^{(\S+)}',元素_tree.getroot().tag)
如果名称空间搜索不是“无”:
返回名称空间_search.group(1)
其他:
一无所获
def__main():
el_tree_hlp=ElementTreeHelper('myxml.xml'))
对于el_tree_hlp.element_tree.iter()中的元素:
如果elem.text和elem.text中的“复制”:
elem.text=无
el_tree_hlp.write('myxml1.xml')
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
__main()