how to insert values to xml variable using python

Viewed 631

i have xml (template) file and i set variables in the xml using {$var}

and i have a list with values for the xml variables.

how can i open (set) my file and set the xml variable to the values from my list ?

<sut>
    <Cdu>
        <class>XXX</class>
        <conn>
            <class>XXX</class>
            <cli>
                <password>{$password}</password>
                <user>{$user}</user>
                <host>{$host}</host>
                <connectOnInit>false</connectOnInit>
            </cli>
        </conn>
        <basic>
            <class>XXX</class>
        </basic>
    </Cdu>
3 Answers

Why not just use f string?

password = 'secret'
user = 'jack'
host = '12.56.78.123'
xml = f'''<sut>
    <Cdu>
        <class>XXX</class>
        <conn>
            <class>XXX</class>
            <cli>
                <password>{password}</password>
                <user>{user}</user>
                <host>{host}</host>
                <connectOnInit>false</connectOnInit>
            </cli>
        </conn>
        <basic>
            <class>XXX</class>
        </basic>
    </Cdu>'''
print(xml)

output

<sut>
    <Cdu>
        <class>XXX</class>
        <conn>
            <class>XXX</class>
            <cli>
                <password>secret</password>
                <user>jack</user>
                <host>12.56.78.123</host>
                <connectOnInit>false</connectOnInit>
            </cli>
        </conn>
        <basic>
            <class>XXX</class>
        </basic>
    </Cdu>

I would use re.sub for this task following way

import re
txt = '''<password>{$password}</password>
<user>{$user}</user>
<host>{$host}</host>'''
data = {"password": "SECRET", "user": "NAME", "host": "LOCAL"}
def replacement(x):
    return data[x.group(1)]
out = re.sub(r'\{\$([^\}]+)\}', replacement, txt)
print(out)

output

<password>SECRET</password>
<user>NAME</user>
<host>LOCAL</host>

Explanation: re.sub second argument might be function which accept Match object. I used regular expression with one capturing group, which content I then use to look up data dict. Note that { and $ and } need escaping as these have special meaning, but we need literal { and $ and }. Disclaimer: I used substring of original for brevity sake.

You can use Template class from standard string module:

from string import Template


password = 'secret'
user = 'jack'
host = '12.56.78.123'

xml_template = '''<sut>
    <Cdu>
        <class>XXX</class>
        <conn>
            <class>XXX</class>
            <cli>
                <password>${password}</password>
                <user>${user}</user>
                <host>${host}</host>
                <connectOnInit>false</connectOnInit>
            </cli>
        </conn>
        <basic>
            <class>XXX</class>
        </basic>
    </Cdu>'''

xml = xml_template.substitute(password=password, user=user, host=host)
print(xml)

There are also safe_substitute method that may be used in case you are not sure about context variables.

Related