Python读取XAML文件中的所有元素并替换一些值

时间:2019-03-27 10:15:23

标签: python xml

所以我有XML文件,在文件中我有很多elements,像这样:

<name search = "read TTT: write">
    <version id = "1.0.0">
        <value>myVal</value>
        <method>myMethod</method>
    </version>
</name>

因此,在每个元素中,我需要找到此search = "read TTT: write"行,并且如果此read TTT: writeread开头,我需要对其进行更改(将读取的内容替换为另一个字符串)。

最好/最快的方法是什么?

更新

因此,如果我有element这样的单词(存在“ read”一词):

<name search = "read TTT: write">
    <version id = "1.0.0">
        <value>myVal</value>
        <method>myMethod</method>
    </version>
</name>

我想用这个替换它(我只替换读的):

<name search = "NewValue TTT: write">
    <version id = "1.0.0">
        <value>myVal</value>
        <method>myMethod</method>
    </version>
</name>

3 个答案:

答案 0 :(得分:1)

使用minidom

from xml.dom import minidom
xmldoc = minidom.parseString(xml)

tags = xmldoc.getElementsByTagName("name")
firstchild = tags[0]
firstchild.attributes["search"].value = 'NewValue TTT: write'

print(xmldoc.toxml())

输出

<?xml version="1.0" ?><name search="NewValue TTT: write">
    <version id="1.0.0">
        <value>myVal</value>
        <method>myMethod</method>
    </version>
</name>

编辑

对于多个标签,只需循环:

xml = '''\
<root>
<name search = "read TTT: write">
    <version id = "1.0.0">
        <value>myVal</value>
        <method>myMethod</method>
    </version>
</name>

<name search = "read2 TTT: write">
    <version id = "2.0.0">
        <value>myVal_2</value>
        <method>myMethod_2</method>
    </version>
</name>
</root>'''

from xml.dom import minidom
xmldoc = minidom.parseString(xml)

tags = xmldoc.getElementsByTagName("name")

for item in tags:
    item.attributes["search"].value = 'NewValue TTT: write'

print(xmldoc.toxml())

输出

<?xml version="1.0" ?><root>
<name search="NewValue TTT: write">
    <version id="1.0.0">
        <value>myVal</value>
        <method>myMethod</method>
    </version>
</name>

<name search="NewValue TTT: write">
    <version id="2.0.0">
        <value>myVal_2</value>
        <method>myMethod_2</method>
    </version>
</name>
</root>

使用beautifulsoup

from bs4 import BeautifulSoup

xml = '''\
<root>
<name search = "read TTT: write">
    <version id = "1.0.0">
        <value>myVal</value>
        <method>myMethod</method>
    </version>
</name>

<name search = "read TTT: write">
    <version id = "2.0.0">
        <value>myVal_2</value>
        <method>myMethod_2</method>
    </version>
</name>
</root>'''

soup = BeautifulSoup(xml , 'lxml')
newVal = "NewValue"

for elem in soup.find_all("name"):
    elem['search'] = elem['search'].replace(str(elem['search']).split(" ")[0], newVal)        
   print(elem)

输出

<name search="NewValue TTT: write">
<version id="1.0.0">
<value>myVal</value>
<method>myMethod</method>
</version>
</name>
<name search="NewValue TTT: write">
<version id="2.0.0">
<value>myVal_2</value>
<method>myMethod_2</method>
</version>
</name>

答案 1 :(得分:1)

from xml.dom.minidom import parse #minidom does the trick
dom = parse('my_file.xml') #read in your file
res = dom.getElementsByTagName('name') #fetch the desired elements in the tree
for element in res: #loop through all 
    if element.getAttribute('search') == 'read TTT: write': #filter for the attribute and value
        element.setAttribute('search', 'New Value TTT: write') #in case of match, replace
with open('my_file_updated.xml','w') as f: #store the file
    f.write(dom.toxml())

答案 2 :(得分:0)

尝试一下:

xml = """<?xml version="1.0" encoding="UTF-8"?>
        <body>
            <name search = "read TTT: write">
                <version id = "1.0.0">
                    <value>myVal</value>
                    <method>myMethod</method>
                </version>
            </name>
            <name search = "read TTT: write">
                <version id = "1.0.0">
                    <value>myVal</value>
                    <method>myMethod</method>
                </version>
            </name>
        </body>
        """

import xml.etree.ElementTree as ET

tree = ET.fromstring(xml)
sh = tree.findall('name')
for name in sh:
    val = name.get("search")
    if str(val) == "read TTT: write":
        name.set('search', 'NewValue TTT: write')


print (ET.tostring(tree))

输出:

   <body>
        <name search = "NewValue TTT: write">
            <version id = "1.0.0">
                <value>myVal</value>
                <method>myMethod</method>
            </version>
        </name>
        <name search = "NewValue TTT: write">
            <version id = "1.0.0">
                <value>myVal</value>
                <method>myMethod</method>
            </version>
         </name>
    </body>