There is simple JSON serialization module with name "simplejson" which easily serializes Python objects to JSON.
I'm looking for similar module which can serialize to XML.
Installation. The dicttoxml module is [published on the Python Package Index](https://pypi.python.org/pypi/dicttoxml), so you can install it using pip or easy_install . That should be all you need to do.
Write XML to the writer object. The writer should have a write() method which matches that of the file object interface. The indent parameter is the indentation of the current node. The addindent parameter is the incremental indentation to use for subnodes of the current one.
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ü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>'''
http://code.activestate.com/recipes/415983/
http://sourceforge.net/projects/pyxser/
http://soapy.sourceforge.net/
http://www.ibm.com/developerworks/webservices/library/ws-pyth5/
http://gnosis.cx/publish/programming/xml_matters_1.txt
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 Document
import copy
class 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With