tef*_*ozi 62 python xml serialization xml-serialization
有一个名为"simplejson"的简单JSON序列化模块,它可以轻松地将Python对象序列化为JSON.
我正在寻找可以序列化为XML的类似模块.
max*_*max 18
有huTools.structured.dict2xml哪些尝试是兼容simplejson的精神.您可以给它提示如何包装嵌套的子结构.如果返回的字符串huTools.structured.dict2et,请检查返回ElementTreeObjects 的文档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>'''
Run Code Online (Sandbox Code Playgroud)
S.L*_*ott 14
http://code.activestate.com/recipes/415983/
http://sourceforge.net/projects/pyxser/
http://www.ibm.com/developerworks/webservices/library/ws-pyth5/
http://gnosis.cx/publish/programming/xml_matters_1.txt
nug*_*ier 11
试试这个.唯一的问题我不使用属性(因为我不喜欢它们)
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()
Run Code Online (Sandbox Code Playgroud)
我写了一个简单的函数,将字典序列化为xml(30行以下).
用法:
mydict = {
'name': 'The Andersson\'s',
'size': 4,
'children': {
'total-age': 62,
'child': [
{
'name': 'Tom',
'sex': 'male',
},
{
'name': 'Betty',
'sex': 'female',
}
]
},
}
print(dict2xml(mydict, 'family'))
Run Code Online (Sandbox Code Playgroud)
结果:
<family name="The Andersson's" size="4">
<children total-age="62">
<child name="Tom" sex="male"/>
<child name="Betty" sex="female"/>
</children>
</family>
Run Code Online (Sandbox Code Playgroud)
完整的源代码(包括示例)可以在https://gist.github.com/reimund/5435343/上找到
注意:此函数将字典条目序列化为属性而不是文本节点.修改它以支持文本将非常容易.
| 归档时间: |
|
| 查看次数: |
66242 次 |
| 最近记录: |