mardi 4 août 2015

Read and write XML file in Python / Flask

I have a bunch of XML configuration files that looks like this

 <list>
        <flip ip="x.x.x.x" port="30016" />
        <geo ip="x.x.x.x" port="11111" />
        <shake ip="x.x.x.x" port="30006" />
            <ip2location ip="x.x.x.x" port="30006" />
        <radar ip="x.x.x.x" port="11111" />
        <fb ip="x.x.x.x" port="22001" />
        <sms ip="x.x.x.x" port="22222" />
        <pn ip="x.x.x.x" port="30001" />
            <db ip="x.x.x.x" pool="300" port="18000" />
        <cache ip="x.x.x.x" pool="100" port="11213" />
        <session ip="x.x.x.x" pool="100" port="11212" />
    </list>

I am writing a flask web-app that can load up these XMLs and display as an HTML web-page, and it also allow the user to edit any field of the XML and then save to the file

What I have done is to parse these XML using xmltodict, so I will get a nested dictionary object, then recursively display them using the flask jinja2 template like this

Simple Form

But there are several problems to it:

1.These XMLs may contain totally different field, so it is not possible to do traditional POST such as in flask request.form[] to update any of the field

2.The parsed dict object is a mixed nested dictionary and list, it is very hard to access and change any value inside

because of these, I don't know how to update the dict object once a user entered a value into the HTML form

Please help if you have some good idea , or any other solution to the requirement

Thanks !

Aucun commentaire:

Enregistrer un commentaire