I am trying to use Python to read in an XML file containing some parameter names and values, e.g.
...
<parameter name='par1'>
<value>24</value>
</parameter>
<parameter name='par2'>
<value>Blue/Red/Green</value>
</parameter>
...
and then pass it a dictionary with the parameter names {‘par1′:’53’,’par2′:’Yellow/Pink/Black’,…} and corresponding new values to replace the old ones in the XML file. The output should then overwrite the original XML file.
At the moment I am converting the XML to a python dictionary and after some element comparison and regular expression handling, writing the output again in XML format.
I am not too happy with this and was wondering whether anyone can recommend a more efficient way of doing it?
Thanks.
My first suggestion is to use lxml or some other Python XML parser rather than using regular expressions. XML is not a language that can be parsed with regular expressions reliably. (If you consistently try to parse XML with regular expressions bad things happen)