所以我有一个XML,它有多行生日,但元素不同. 例如,在我的XML中有这样几行代码:
<date-of-birth>12-3-1998</date-of-birth>
<date-of-birth>12-3-1998</date-of-birth>
<date-of-birth>12-3-1998</date-of-birth>
<date-of-birth>31-7-1941</date-of-birth>
<date-of-birth>23-11-1965</date-of-birth>
我想把只有道布"12-3-1998"
的台词改成道布"14-11-2001"
,其他的保持不变,但我在努力寻找如何做到这一点而不全部更改或不更改.
我试着这样做:
import xml.etree.ElementTree as ET
xml_tree = ET.parse(TestXML.xml)
root = xml_tree.getroot()
for DOB in root.findall(".//{*}12-3-1998"):
DOB.text = "14-11-2001"
ET.tostring(root)
但是我的root.findall(".//{*}12-3-1998")
没有发现任何东西,所以我所有的DOB都保持不变
我试着做了以下几件事,但它改变了我所有的道布元素,我只想改变那些有12-3-1998的元素:
import xml.etree.ElementTree as ET
xml_tree = ET.parse(TestXML.xml)
root = xml_tree.getroot()
for DOB in root.findall(".//{*}date-of-birth"):
DOB.text = "14-11-2001"
ET.tostring(root)
所以我想知道是否有一种方法可以过滤我想要更改的特定的道布,无论它是使用ElementTree还是另一个Python库?