Serialize Python dictionary to XML [closed] Serialize Python dictionary to XML [closed] xml xml

Serialize Python dictionary to XML [closed]


There is huTools.structured.dict2xml which tries to be compatible to simplejson in spirit. You can give it hints how to wrap nested sub-structures. Check the documentation for huTools.structured.dict2et which returns ElementTree Objects instead if the strings returned by dict2xml.

>>> data = {"kommiauftragsnr":2103839, "anliefertermin":"2009-11-25", "prioritaet": 7,... "ort": u"Hücksenwagen",... "positionen": [{"menge": 12, "artnr": "14640/XL", "posnr": 1},],... "versandeinweisungen": [{"guid": "2103839-XalE", "bezeichner": "avisierung48h",...                          "anweisung": "48h vor Anlieferung unter 0900-LOGISTIK avisieren"},... ]}>>> print ET.tostring(dict2et(data, 'kommiauftrag',... listnames={'positionen': 'position', 'versandeinweisungen': 'versandeinweisung'}))'''<kommiauftrag><anliefertermin>2009-11-25</anliefertermin><positionen>    <position>        <posnr>1</posnr>        <menge>12</menge>        <artnr>14640/XL</artnr>    </position></positionen><ort>H&#xC3;&#xBC;cksenwagen</ort><versandeinweisungen>    <versandeinweisung>        <bezeichner>avisierung48h</bezeichner>        <anweisung>48h vor Anlieferung unter 0900-LOGISTIK avisieren</anweisung>        <guid>2103839-XalE</guid>    </versandeinweisung></versandeinweisungen><prioritaet>7</prioritaet><kommiauftragsnr>2103839</kommiauftragsnr></kommiauftrag>'''


try this one. only problem I don't use attributes (because i dont like them)
dict2xml on pynuggets.wordpress.com
dict2xml on activestate

from xml.dom.minidom import Documentimport copyclass dict2xml(object):    doc     = Document()    def __init__(self, structure):        if len(structure) == 1:            rootName    = str(structure.keys()[0])            self.root   = self.doc.createElement(rootName)            self.doc.appendChild(self.root)            self.build(self.root, structure[rootName])    def build(self, father, structure):        if type(structure) == dict:            for k in structure:                tag = self.doc.createElement(k)                father.appendChild(tag)                self.build(tag, structure[k])        elif type(structure) == list:            grandFather = father.parentNode            tagName     = father.tagName            grandFather.removeChild(father)            for l in structure:                tag = self.doc.createElement(tagName)                self.build(tag, l)                grandFather.appendChild(tag)        else:            data    = str(structure)            tag     = self.doc.createTextNode(data)            father.appendChild(tag)    def display(self):        print self.doc.toprettyxml(indent="  ")if __name__ == '__main__':    example = {'auftrag':{"kommiauftragsnr":2103839, "anliefertermin":"2009-11-25", "prioritaet": 7,"ort": u"Huecksenwagen","positionen": [{"menge": 12, "artnr": "14640/XL", "posnr": 1},],"versandeinweisungen": [{"guid": "2103839-XalE", "bezeichner": "avisierung48h","anweisung": "48h vor Anlieferung unter 0900-LOGISTIK avisieren"},]}}    xml = dict2xml(example)    xml.display()