XML 库类似于 simplejson/json? - Python

发布于 2024-09-04 17:04:05 字数 186 浏览 10 评论 0原文

是否有一个与 simplejson 类似的库,它可以实现数据与 XML 之间的快速序列化。

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

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

有什么想法吗?

is there a similar library to simplejson, which would enable quick serialization of data to and from XML.

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

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

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情绪失控 2024-09-11 17:04:05

您不会发现 xml 与 json 一样一致,因为 xml 不了解数据类型。这取决于您是否遵循约定或强制遵守 xml 架构文件。

话虽这么说,如果您愿意接受 XML-RPC 数据结构映射和一些限制,请查看 Python 标准库中的 xmlrpclib 包:

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'},)
>>> 

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.

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:

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'},)
>>> 
兮子 2024-09-11 17:04:05

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

You can look how they have done it in Django: xml_serializer.py and tailor this to your needs.

青丝拂面 2024-09-11 17:04:05

我不知道有一个。除非 xmlrpc 计数...
如果您正在考虑自己推出:使用 ElementTree 执行任何操作与大多数其他 XML 库相比,这是一种乐趣。

但是,由于您最终可能会得到非标准化的表示,因此您需要控制双方,对吧?
那么为什么不直接选择 json, pickle 或者已经存在的东西?

如果您想使用 xmlrpclib 模块:

xmlrpclib.dumps(data)

Forest 提到了 xmlrpclib 中的限制,这是一个很好的观点。我自己见过的一些:整数不能超过 2^31-1,否则库会抱怨。 “无”值通常不好,但您可以解决这个问题。
可能还有其他限制。

除此之外,xmlrpc 协议非常冗长。如果您需要担心发送多少数据,那么它不是最好的。但没有任何 XML 版本会非常高效。

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.

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?

In case you want to use the xmlrpclib module:

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.

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.

天暗了我发光 2024-09-11 17:04:05

xml 不像 json 那样直接,因为 xml 和 python 的数据类型之间没有“类型映射”。哎呀,XML 数据可以是任何内容,就像在相应的 XSL 中映射的那样。

至于你最关心的API,我推荐Element Tree

有关使用元素树解析 XML 的好教程,我建议您参考 Mark Pilgrim's Dive into Python3

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.

As for the API is concerned, which you are mostly bothered about, I recommend Element Tree

For a good tutorial on Parsing XML using Element Tree, I refer you to Mark Pilgrim's Dive into Python3

挖个坑埋了你 2024-09-11 17:04:05

lxml 怎么样?

What about lxml?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文