My XML file test.xml contains the following tags
<?xml version="1.0" encoding="ISO-8859-1"?>
<AppName>
<out>This is a sample output with <test>default</test> text </out>
<AppName>
I have written a python code which does the following till now:
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
The output of the following program is This is a sample output with <test>default</test> text
You will also notice i have a list with list = {'test':'example'} defined.
I want to check if in the out there is a tag which is listed in the list, will be replaced with the corresponding value, else the default value.
In this case, the output should be:
This is a sample output with example text
This will do more or less what you want:
Notice that I used
replacementsrather thanlist. In python terms, it’s a dictionary, not a list, so that’s a confusing name. It’s also a builtin function, so you should avoid using it as a name.If you want a dom object rather than just text, you’ll need to take a different approach.