简体   繁体   English

XML库类似于simplejson / json? - Python

[英]XML library similar to simplejson/json? - Python

is there a similar library to simplejson, which would enable quick serialization of data to and from XML. 是否有类似于simplejson的库,它可以实现与XML的快速序列化数据。

e.g. json.loads('{vol:'III', title:'Magical Unicorn'}')

e.g. json.dumps([1,2,3,4,5])

Any ideas? 有任何想法吗?

You're not going to find anything for xml as consistent as json, because xml doesn't know about data types. 你不会为xml找到任何与json一致的东西,因为xml不知道数据类型。 It depends on you to follow conventions or enforce adherence to an xml schema file. 这取决于您遵循约定或强制遵守xml架构文件。

That being said, if you're willing to accept the XML-RPC data structure mapping and a few limitations, check out the xmlrpclib package that lives in the Python standard library: 话虽这么说,如果您愿意接受XML-RPC数据结构映射和一些限制,请查看存在于Python标准库中的xmlrpclib包:

http://docs.python.org/library/xmlrpclib.html#convenience-functions http://docs.python.org/library/xmlrpclib.html#convenience-functions

>>> import xmlrpclib
>>> s = xmlrpclib.dumps( ({'vol':'III', 'title':'Magical Unicorn'},))
>>> print s
<params>
<param>
<value><struct>
<member>
<name>vol</name>
<value><string>III</string></value>
</member>
<member>
<name>title</name>
<value><string>Magical Unicorn</string></value>
</member>
</struct></value>
</param>
</params>

>>> xmlrpclib.loads(s)[0]
({'vol': 'III', 'title': 'Magical Unicorn'},)
>>> 

你可以看看他们是如何在Django中完成它的: xml_serializer.py并根据你的需要定制它。

I don't know of one. 我不知道一个。 Unless xmlrpc counts... In case you are thinking about rolling your own: Doing anything with ElementTree is a pleasure, compared with most other XML libraries. 除非xmlrpc计算...如果你正在考虑自己动手:与大多数其他XML库相比,使用ElementTree做任何事情都是一种乐趣。

But, since you'd probably end up with a representation that would be non-standarized, you would need to control both sides, right? 但是,既然你可能最终会得到一个非标准化的代表,你需要控制双方,对吗? Then why not just pick json , pickle or something that is already there? 那么为什么不选择jsonpickle或已经存在的东西呢?

In case you want to use the xmlrpclib module: 如果您想使用xmlrpclib模块:

xmlrpclib.dumps(data)

Forest mentions limitations in xmlrpclib, which is a good point. Forest提到了xmlrpclib中的限制,这是一个很好的观点。 Some that I've seen myself: Integers can't be more than 2^31-1 or the library will complain. 有些我自己看过:整数不能超过2 ^ 31-1或者图书馆会抱怨。 "None" values typically aren't OK, but you can get around that. “无”值通常不合适,但你可以解决这个问题。 There are probably other limitations as well. 也可能存在其他限制。

Apart from that, the xmlrpc-protocol is pretty verbose. 除此之外,xmlrpc协议非常详细。 if you need to worry about how much data is sent, it's not the best one. 如果您需要担心发送了多少数据,那么这不是最好的数据。 But no XML version will be very efficient. 但是没有XML版本会非常有效。

It's not as straight forward with xml, as it is with json because, there is no "type mapping" between the datatypes of xml and python. 使用xml并不像json那样直接,因为xml和python的数据类型之间没有“类型映射”。 Heck XML data can be anything, as mapped within the corresponding XSL. Heck XML数据可以是任何东西,映射在相应的XSL中。

As for the API is concerned, which you are mostly bothered about, I recommend Element Tree 至于API,你最关心的是,我推荐Element Tree

For a good tutorial on Parsing XML using Element Tree, I refer you to Mark Pilgrim's Dive into Python3 有关使用Element Tree解析XML的好教程,我将向您介绍Mark Pilgrim的Dive into Python3

那么lxml呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM