Python:如何编辑XML文件的元素?

前端之家收集整理的这篇文章主要介绍了Python:如何编辑XML文件的元素? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个XML文件,该文件包含许多命名元素,每个元素下面都有几个节点:

<name search = "select ARG: write">
    <version id = "1.0.0">
        <value>myVal</value>
        <method>myMethod</method>
    </version>
</name>
<name search = "select ARG: bla">
    <version id = "2.0.0">
        <value>myVal</value>
        <method>myMethod</method>
    </version>
</name>

我想搜索这个XML文件,并且如果该名称搜索值以select ARG开头(在我的示例中,第一个值是select ARG:write),我想创建这个新的命名元素,但是这次是选择ARG的值:应选择写ARG:写.这是我尝试过的:

from xml.dom import minidom

xamlfile = r'C:\file.xml'
newxamlfile = r'C:\new.xml'

dom = minidom.parse(xamlfile)

# Fetch the desired elements in the tree.
res = dom.getElementsByTagName('name')

# Loop through all.
for element in res:
    search_name_value = element.getAttribute('search')

    # Filter for the attribute and value.
    if search_name_value.startswith('select ARG:'):
        # In case of match,replace.
        element.setAttribute('search_name',search_name_value.replace('select ARG:','selected ARG:'))

# Store the file.
with open(newxamlfile,'w') as f:
    f.write(dom.toxml())

在这里,我替换了所需的字符串,而不添加新的字符串,而是编辑所需的元素,而不是创建新的元素并将其添加文件中.

有什么建议怎么做?

更新

这是我之前的文件

<project version="4">
<name search="select ARG: write">
    <version id="1.0.0">
        <value>myVal</value>
        <method>myMethod</method>
    </version>
</name>
<name search="select ARG: bla">
    <version id="2.0.0">
        <value>myVal</value>
        <method>myMethod</method>
    </version>
</name>
</project>

这就是我想要我的文件的方式:

<project version="4">
<name search="select ARG: write">
    <version id="1.0.0">
        <value>myVal</value>
        <method>myMethod</method>
    </version>
</name>
<name search="selected ARG: write">
    <version id="1.0.0">
        <value>myVal</value>
        <method>myMethod</method>
    </version>
</name>
<name search="select ARG: bla">
    <version id="2.0.0">
        <value>myVal</value>
        <method>myMethod</method>
    </version>
</name>
<name search="selected ARG: bla">
    <version id="2.0.0">
        <value>myVal</value>
        <method>myMethod</method>
    </version>
</name>
</project>

编辑

根据@DirtyBit的建议:

xmldoc = minidom.parse(xamlfile)

tags = xmldoc.getElementsByTagName("name")

for item in tags:
    str = item.attributes["search"].value
    if 'select ARG' in str:
        item.attributes["search"].value = item.attributes["search"].value.replace('select ARG','selected ARG')

with open(xamlfile,"a+") as f:
    xmldoc.writexml(f)

这工作正常,但我有2个问题:

>如您所见,我添加了if语句,因为我想复制并仅在选择ARG的值中创建新节点(并将其替换为所选的ARG),而不复制没有达到此条件的其他节点.
>在新XML文件的中间,我有这行:

< / element><?xml version =“ 1.0”?>< element>

最佳答案
下面的代码克隆所需的元素,并将其附加到文档末尾.

55394530.xml是一个文件,其中包含从示例XML中获取的数据

import xml.etree.ElementTree as ET
import copy
from xml.dom import minidom

tree = ET.parse('55394530.xml')

names_to_duplicate = [e for e in tree.findall('.//name') if e.attrib.get('search').startswith('select ARG:')]
for name in names_to_duplicate:
    clone = copy.deepcopy(name)
    clone.attrib['search'] = clone.attrib['search'].replace('select','selected')
    tree.getroot().append(clone)

xmlstr = minidom.parseString(ET.tostring(tree.getroot())).toprettyxml()
with open('out.xml','w') as out:
    out.write(xmlstr)

输出

<element>
    <name search="select ARG: 123">
        <version id="1.1.1">
            <value>bla</value>
            <method>blabla</method>
        </version>
    </name>
    <name search="select ARG: 456">
        <version id="1.1.1">
            <value>bla</value>
            <method>blabla</method>
        </version>
        <version id="1.1.1">
            <value>bla</value>
            <method>blabla</method>
        </version>
        <version id="1.1.1">
            <value>bla</value>
            <method>blabla</method>
        </version>
    </name>
    <name search="text ARG: 789">
        <version id="1.1.1">
            <value>bla</value>
            <method>blabla</method>
        </version>
    </name>
    <name search="foo ARG: 444">
        <version id="1.1.1">
             <value>bla</value>
            <method>blabla</method>
        </version>
    </name>
    <name search="test ARG: Cancel">
        <version id="1.1.1">
            <value>bla</value>
            <method>blabla</method>
        </version>
    </name>
<name search="selected ARG: 123">
        <version id="1.1.1">
            <value>bla</value>
            <method>blabla</method>
        </version>
    </name>
    <name search="selected ARG: 456">
        <version id="1.1.1">
            <value>bla</value>
            <method>blabla</method>
        </version>
        <version id="1.1.1">
            <value>bla</value>
            <method>blabla</method>
        </version>
        <version id="1.1.1">
            <value>bla</value>
            <method>blabla</method>
        </version>
    </name>
    </element>
原文链接:https://www.f2er.com/python/533255.html

猜你在找的Python相关文章