0

我只想使用python dom或elementtree删除xml文件的文本值。我的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

<?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>
4

2 回答 2

0

使用 elementtree,尝试以下方式:

for tag in tree.findAll("Command"):
    tag.text = None
于 2012-09-17T05:44:58.993 回答
0

我只是扩展从stackoverflow获得的解决方案;我经常使用它...只需在“__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()
于 2012-09-17T14:24:42.823 回答