从XML标签中删除文本值python

2024-06-27 09:34:40 发布

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

我只想使用pythondom或元素树。我的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>

我想要的是清空包含副本的命令标记过程。和保持所有其他命令标记没有复制过程,如下面的xml所示

^{pr2}$

Tags: 标记debug命令projectxmlconditionconfigurationcommand
2条回答

我只是扩展stackoverflow得到的解决方案; 我经常用它。。。 只需在'\uu 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()

使用elementtree,可以按照以下方法进行尝试:

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

相关问题 更多 >