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>
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())
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>
添加回答
举报