简体   繁体   中英

Parsing XML tags through Python and replace it using xml.dom.minidom

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:

from xml.dom.minidom import parseString, getDOMImplementation

test_xml = '''<?xml version="1.0" encoding="ISO-8859-1"?>
<AppName>
    <out>This is a sample output with <test>default</test> text </out>
</AppName>'''

replacements = {'test':'example'}
dom = parseString(test_xml)
if (len(dom.getElementsByTagName('out'))!=0):
    xmlTag = dom.getElementsByTagName('out')[0]
    children =  xmlTag.childNodes
    text = ""
    for c in children:
        if c.nodeType == c.TEXT_NODE:
            text += c.data
        else:
            if c.nodeName in replacements.keys():
                text += replacements[c.nodeName]
            else: # not text, nor a listed tag
                text += c.toxml()
    print text

Notice that I used replacements rather than list . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM