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?
有任何想法吗?
5 个解决方案
#1
3
You're not going to find anything for xml as consistent as json, because xml doesn't know about data types. It depends on you to follow conventions or enforce adherence to an xml schema file.
你不会为xml找到与json一致的任何东西,因为xml不知道数据类型。这取决于您遵循约定或强制遵守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'},)
>>>
#2
3
You can look how they have done it in Django: xml_serializer.py and tailor this to your needs.
你可以看看他们是如何在Django中完成它的:xml_serializer.py并根据你的需要定制它。
#3
2
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?
但是,既然你可能最终会得到一个非标准化的代表,你需要控制双方,对吗?那么为什么不选择json,pickle或已经存在的东西呢?
In case you want to use the xmlrpclib module:
如果您想使用xmlrpclib模块:
xmlrpclib.dumps(data)
Forest mentions limitations in xmlrpclib, which is a good point. Some that I've seen myself: Integers can't be more than 2^31-1 or the library will complain. "None" values typically aren't OK, but you can get around that. There are probably other limitations as well.
Forest提到了xmlrpclib中的限制,这是一个很好的观点。我自己看到的一些:整数不能超过2 ^ 31-1或者图书馆会抱怨。 “无”值通常不合适,但你可以解决这个问题。也可能存在其他限制。
Apart from that, the xmlrpc-protocol is pretty verbose. 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.
除此之外,xmlrpc协议非常详细。如果您需要担心发送了多少数据,那么这不是最好的数据。但是没有XML版本会非常有效。
#4
2
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. Heck XML data can be anything, as mapped within the corresponding XSL.
使用xml并不像json那样直接,因为xml和python的数据类型之间没有“类型映射”。 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
#1
3
You're not going to find anything for xml as consistent as json, because xml doesn't know about data types. It depends on you to follow conventions or enforce adherence to an xml schema file.
你不会为xml找到与json一致的任何东西,因为xml不知道数据类型。这取决于您遵循约定或强制遵守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'},)
>>>
#2
3
You can look how they have done it in Django: xml_serializer.py and tailor this to your needs.
你可以看看他们是如何在Django中完成它的:xml_serializer.py并根据你的需要定制它。
#3
2
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?
但是,既然你可能最终会得到一个非标准化的代表,你需要控制双方,对吗?那么为什么不选择json,pickle或已经存在的东西呢?
In case you want to use the xmlrpclib module:
如果您想使用xmlrpclib模块:
xmlrpclib.dumps(data)
Forest mentions limitations in xmlrpclib, which is a good point. Some that I've seen myself: Integers can't be more than 2^31-1 or the library will complain. "None" values typically aren't OK, but you can get around that. There are probably other limitations as well.
Forest提到了xmlrpclib中的限制,这是一个很好的观点。我自己看到的一些:整数不能超过2 ^ 31-1或者图书馆会抱怨。 “无”值通常不合适,但你可以解决这个问题。也可能存在其他限制。
Apart from that, the xmlrpc-protocol is pretty verbose. 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.
除此之外,xmlrpc协议非常详细。如果您需要担心发送了多少数据,那么这不是最好的数据。但是没有XML版本会非常有效。
#4
2
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. Heck XML data can be anything, as mapped within the corresponding XSL.
使用xml并不像json那样直接,因为xml和python的数据类型之间没有“类型映射”。 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