1

I want to convert xml (qml) file to dictionary, then convert this dictionary to new xml. (Also want to edit something, but first lets just convert it in both way without it.)

I'm using xmltodict module to xml->dict and then dicttoxml module,dom parse to string and file.write() to dict->xml.

    xml = open("path",  "r")
    org_xml = xml.read()
    dict_xml = xmltodict.parse(org_xml)

    new_xml = dicttoxml.dicttoxml(dict_xml, custom_root = 'qgis')
    dom = (parseString(new_xml)).toprettyxml()

    with open("path/new.qml", 'a') as file:
        file.write(dom.encode("utf-8"))

The problem is, that while converting I 'loose' something. First file is a categorized symbol style tempale and the second is single symbol.

Anyone tried conversion like this and can help?

2

I just realized that the problem was in dictoxml or dom parsing. I changed my code a bit everything works nicely:

xml = open(path, "r")
org_xml = xml.read()
dict_xml = xmltodict.parse(org_xml, process_namespaces=True)

out = xmltodict.unparse(dict_xml, pretty=True)
with open("path/new.qml", 'a') as file:
    file.write(out.encode('utf-8'))
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.