我有以下xml:
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria"/>
<neighbor direction="W" name="Switzerland"/>
</country>
我想用“德国”代替“列支敦士登”,结果应该是:
<country name="Germany">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria"/>
<neighbor direction="W" name="Switzerland"/>
</country>
到目前为止,我认为:
from xml.dom import minidom
xmldoc = minidom.parse('C:/Users/Torah/Desktop/country.xml')
print xmldoc.toxml()
country = xmldoc.getElementsByTagName("country")
firstchild = country[0]
print firstchild.attributes["name"].value
#simple string mathod to replace
print firstchild.attributes["name"].value.replace("Liechtenstein", "Germany")
print xmldoc.toxml()
西缅的台词确实管用。
或者,您可以这样做:
以下行实际上不会更改XML:
它只获取该值,用该字符串中的德国替换列支敦士登并打印该字符串。它不会修改XML文档中的值。
您应该直接分配一个新值:
相关问题 更多 >
编程相关推荐