XML 与 YAML 与 JSON [英] XML vs YAML vs JSON

查看:24
本文介绍了XML 与 YAML 与 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我从头开始一个项目,它不依赖于任何其他项目.我想使用一种格式来存储提要,例如 XML,因为 XML 不是同类格式中唯一可用的格式,我想知道:为什么我要选择一种格式?

Assuming I'm starting a project from scratch, which is not dependent on any other project. I would like to use a format to store feeds, something like XML, since XML is not the only available format of its kind, I would like to know: why should I choose one over the rest?

我将使用 perl.

Feed"是对产品的描述(名称、价格、类型、简短描述,最多 120 个字).

'Feed' is a description of a product (name, price, type, short description, up to 120 words).

推荐答案

如果不了解更多,我们无法真正回答这个问题.仅仅因为您目前不依赖任何其他项目,您是否有可能在未来的某个时候与他们互动?如果是这样,他们更喜欢什么技术?在 BBC,我们有一些仅限 JSON"的项目,结果发现想要访问我们的 API 的 Java 开发人员乞求我们提供一个简单的 XML API,因为他们有许多围绕 XML 构建的工具.他们甚至不关心命名空间、属性或其他任何东西;他们只是想要那些尖括号.

We can't really answer that without knowing a lot more. Just because you're not currently dependent on any other projects, are you likely to interact with them at some point in the future? If so, what technologies do they prefer? At the BBC, we've had some projects "JSON-only", only to find out that Java developers who wanted to access our API were begging us to provide a simple XML API simply because they have so many tool built around XML. They didn't even care about namespaces, attributes, or anything else; they just wanted those angle-brackets.

至于存储提要",我也不确定您在那里的意思.您解释了提要中的数据,但接下来您打算如何处理这些提要?解析它们?缓存并保留它们?把它们写成楔形文字片?:)

As for "storing feeds", I also not sure what you mean there. You explain the data in the feed, but what are you then going to do with those feeds? Parse them? Cache and reserve them? Write them out to cuneiform tablets? :)

我听起来像您真正想要的是一个数据库,并且您希望将数据保存在那里,然后将其序列化为 JSON/YAML/XML 或任何您想要的格式.我的建议是能够将数据提取到 Perl 数据结构中,然后让格式化程序"知道如何将该数据结构序列化为所需的输出.这样你就可以序列化为 JSON,如果这还不够好,可以轻松切换到 YAML 或其他东西.事实上,如果其他人需要你的数据(单向数据往往没有用),他们可以要求 JSON、YAML、XML 或其他任何东西.您拥有更大的灵活性,并且不会受到预先做出的决定的束缚.

I sounds like what you actually want is a database and you want to persist the data there and later make it serialisable as JSON/YAML/XML or whatever your desired format is. What I'd recommend is to be able to pull the data out into a Perl data structure and then have "formatters" which know how to serialise that data structure to the desired output. That way you can serialise to, say, JSON, and later if that's not good enough, easily switch to YAML or something else. In fact, if others need your data (one-way data tends not to be useful), they can ask for JSON, YAML, XML or whatever. You have more flexibility and aren't tied into a decision that you made up front.

话虽如此,我不了解您的系统,因此很难说正确的做法是什么.此外,并不是说 JSON 和 YAML 不能与 XML 完全互换.细微的差异可以而且会让你失望.

That being said, I don't know your system, so it's tough to say what the right thing to do is. Also, not that JSON and YAML aren't exactly interchangeable with XML. Subtle differences can and will trip you up.

这篇关于XML 与 YAML 与 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆