Serialize Python dictionary to XML
有一个简单的JSON序列化模块,名称为" simplejson",可轻松将Python对象序列化为JSON。
我正在寻找可以序列化为XML的类似模块。
本质上有
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | >>> 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> 2009-11-25</anliefertermin> <positionen> <position> <posnr>1</posnr> <menge>12</menge> 14640/XL</artnr> </position> </positionen> <ort>H cksenwagen</ort> <versandeinweisungen> <versandeinweisung> <bezeichner>avisierung48h</bezeichner> 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
试试这个。唯一的问题是我不使用属性(因为我不喜欢它们)
pynuggets.wordpress.com上的dict2xml
active2上的dict2xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 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() |
我写了一个简单的函数,将字典序列化为xml(30行以下)。
用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | mydict = { 'name': 'The Andersson\'s', 'size': 4, 'children': { 'total-age': 62, 'child': [ { 'name': 'Tom', 'sex': 'male', }, { 'name': 'Betty', 'sex': 'female', } ] }, } print(dict2xml(mydict, 'family')) |
结果:
1 2 3 4 5 6 | <family name="The Andersson's" size="4"> <children total-age="62"> <child name="Tom" sex="male"/> <child name="Betty" sex="female"/> </children> </family> |
完整的源代码(包括示例)可以在https://gist.github.com/reimund/5435343/上找到。
注意:此功能会将序列表条目序列化为属性,而不是文本节点。对其进行修改以支持文本将非常容易。
Python中的大多数对象都表示为下面的字典:
1 2 3 4 5 6 | >>> class Fred(object) : ... def __init__(self, n) : self.n = n ... >>> a = Fred(100) >>> print a.__dict__ {'n': 100} |
因此,这类似于询问如何将字典转换为XML。
有以下工具可将dict与XML相互转换:
http://www.picklingtools.com
这是一个简单的示例:
1 2 3 4 5 6 7 8 9 10 11 | >>> import xmltools >>> d = {'a':1, 'b':2.2, 'c':'three' } >>> xx = xmltools.WriteToXMLString(d) >>> print xx <?xml version="1.0" encoding="UTF-8"?> <top> 1 2.2 <c>three</c> </top> |
网站上有很多文档显示了示例:
XML工具手册
很难在字典和XML之间"精确地"转换:什么是列表?您如何处理属性?您如何处理数字键?其中许多问题已得到解决
并在上面的XML工具文档中进行了讨论。
速度对您来说重要吗?还是易用性很重要?
有一个纯C ++模块(全部用C ++编写),一个纯Python模块(全部用Python编写)和一个Python C扩展模块(用C ++编写,但包装起来以便Python可以调用它)。 C ++和Python C Extension模块的速度要快几个数量级,但是当然需要编译才能开始。 Python模块应该可以正常工作,但是速度较慢: