为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

幕布斯7119047 2021-12-17 16:48:44
所以我有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: write开始与read我需要改变它(用一个字符串替换读)。最好的\最快的方法是什么?更新因此,如果我有element这样的情况(存在“阅读”一词):<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 回答

?
杨魅力

TA贡献1811条经验 获得超6个赞

使用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>


查看完整回答
反对 回复 2021-12-17
?
慕桂英546537

TA贡献1848条经验 获得超10个赞

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())


查看完整回答
反对 回复 2021-12-17
?
万千封印

TA贡献1891条经验 获得超3个赞

试试这个:


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>


查看完整回答
反对 回复 2021-12-17
  • 3 回答
  • 0 关注
  • 316 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信