我的 XML 文件 test.xml 包含以下标签
<?xml version="1.0" encoding="ISO-8859-1"?>
<AppName>
<out>This is a sample output with <test>default</test> text </out>
<AppName>
到目前为止,我已经编写了一个 python 代码,它执行以下操作:
from xml.dom.minidom import parseString
list = {'test':'example'}
file = open('test.xml','r')
data = file.read()
file.close()
dom = parseString(data)
if (len(dom.getElementsByTagName('out'))!=0):
xmlTag = dom.getElementsByTagName('out')[0].toxml()
out = xmlTag.replace('<out>','').replace('</out>','')
print out
以下程序的输出是This is a sample output with <test>default</test> text
您还会注意到我有一个已list = {'test':'example'}
定义的列表。
我想检查 out 中是否有列表中列出的标签,将替换为相应的值,否则为默认值。
在这种情况下,输出应该是:
This is a sample output with example text