XML 与 YAML 与 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3951047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML vs YAML vs JSON
提问by snoofkin
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?
假设我从头开始一个项目,它不依赖于任何其他项目。我想使用一种格式来存储提要,例如 XML,因为 XML 不是同类格式中唯一可用的格式,我想知道:为什么我要选择一种格式?
I will be using perl.
我将使用 perl。
'Feed' is a description of a product (name, price, type, short description, up to 120 words).
“Feed”是对产品的描述(名称、价格、类型、简短描述,最多 120 个字)。
回答by Ovid
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 beggingus 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.
如果不了解更多,我们无法真正回答这个问题。仅仅因为您目前不依赖任何其他项目,您是否有可能在未来的某个时候与他们互动?如果是,他们更喜欢什么技术?在 BBC,我们有一些“仅限 JSON”的项目,结果发现想要访问我们的 API 的 Java 开发人员恳求我们提供一个简单的 XML API,仅仅因为他们有很多围绕 XML 构建的工具。他们甚至不关心命名空间、属性或其他任何东西;他们只是想要那些尖括号。
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? :)
至于“存储提要”,我也不确定您在那里的意思。您解释了提要中的数据,但是您打算如何处理这些提要?解析它们?缓存并保留它们?把它们写成楔形文字片?:)
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 或任何您想要的格式。我的建议是能够将数据提取到 Perl 数据结构中,然后让“格式化程序”知道如何将该数据结构序列化为所需的输出。这样你就可以序列化为 JSON,如果这还不够好,可以轻松切换到 YAML 或其他东西。事实上,如果其他人需要你的数据(单向数据往往没有用),他们可以要求 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.
话虽如此,我不了解您的系统,因此很难说正确的做法是什么。此外,并不是说 JSON 和 YAML 不能与 XML 完全互换。细微的差异可以而且会绊倒你。
回答by Joshua Fox
Each will do the job.
每个人都会做这项工作。
JSON has the advantage of super-easy parsing in JavaScript, though you'll probably have to find and introduce a library in other languages.
JSON 具有在 JavaScript 中非常容易解析的优势,尽管您可能需要找到并引入其他语言的库。
XML has the advantage that more languages bundle the relevant libraries, and is useful for the storage you mention. So, it is valuable for passing around through different systems, both "in-motion" and "at-rest".
XML 的优点是更多的语言捆绑了相关的库,对你提到的存储很有用。因此,它对于通过不同的系统(“运动中”和“静止”)很有价值。
YAML has libraries for all languages, but is somewhat less commonly used, so you are more likely to have to find and introduce a library.
YAML 具有适用于所有语言的库,但不太常用,因此您更有可能必须找到并引入库。
回答by Janus Troelsen
I think XML has been thoroughly explained by the others. However, YAML and JSON are both elegant languages, and they are not as similar as you might believe at first glance.
我认为其他人已经彻底解释了 XML。但是,YAML 和 JSON 都是优雅的语言,它们并不像您乍一看所认为的那样相似。
Some of the particularities about YAML
YAML 的一些特殊性
References
- person: &id002 name: James age: 5.0 - person: *id001The second person is an associative array equal to the first.
Casting data types
foobar: !!str 123foobar is "123" (type string).
Uncommon data types not supported by every implementation
Wikipedia:
Particularly interesting ones [...] are sets, ordered maps, timestamps, and hexadecimal.
参考
- person: &id002 name: James age: 5.0 - person: *id001第二个人是一个与第一个人相等的关联数组。
转换数据类型
foobar: !!str 123foobar 是“123”(字符串类型)。
并非每个实现都支持不常见的数据类型
维基百科:
特别有趣的 [...] 是集合、有序映射、时间戳和十六进制。
Therefore, I consider JSON a lot simpler.
因此,我认为 JSON 简单得多。
An argument for JSON
JSON 的参数
Not just for JavaScript
不仅适用于 JavaScript
While it might seem stupid to use the "JavaScript Object Notation" for your application if you don't use JavaScript, you should really consider it anyway, because the data types offered in JSON are probably the most common ones in your language too.
如果您不使用 JavaScript,那么在您的应用程序中使用“JavaScript 对象表示法”似乎很愚蠢,但无论如何您都应该考虑一下,因为 JSON 中提供的数据类型可能也是您的语言中最常见的数据类型。
Readable, even if the whitespace is optional
可读,即使空格是可选的
I think JSON is very readable once prettified, which is very easy to do. YAML is difficult to make compact, since it relies on the whitespace. Granted, you should rely on compression for saving bandwidth. The references in YAML might save you a few bytes, but they add a lot of complexity. If you are really dealing with amounts of data that makes it important to avoid duplication, I'd suggest solving that problem on a whole other level. Not even XML supports these kind of macros.
我认为 JSON 一经美化就非常易读,这很容易做到。YAML 很难压缩,因为它依赖于空格。当然,您应该依靠压缩来节省带宽。YAML 中的引用可能会为您节省几个字节,但它们会增加很多复杂性。如果您真的要处理大量数据,这使得避免重复变得很重要,我建议您在另一个层面上解决该问题。甚至 XML 也不支持这些宏。
回答by Robert Rossney
Choose XML if you need to interoperate with systems you don't control (XML Schema is invaluable here), if you will be transforming the data extensively into text, HTML, or XML (haters notwithstanding, XSLT is peerless), if your data includes a lot of text markup, if your data needs to be human-editable (haters notwithstanding, editable XML that's validated against a schema is a pretty good tool for a lot of jobs), and/or if you need to interoperate any of the myriad of tools and technologies that work with XML.
如果您需要与您无法控制的系统进行互操作(XML Schema 在这里非常宝贵),如果您要将数据广泛地转换为文本、HTML 或 XML(尽管讨厌,XSLT 是无与伦比的),如果您的数据包括大量文本标记,如果您的数据需要人工编辑(尽管讨厌,根据模式验证的可编辑 XML 是许多工作的非常好的工具),和/或如果您需要互操作无数使用 XML 的工具和技术。
Choose JSON if you really can't be bothered with any of the above.
如果您真的不会为上述任何一项烦恼,请选择 JSON。
Choose YAML if you're working in an environment that's got a lot of YAML support.
如果您在获得大量 YAML 支持的环境中工作,请选择 YAML。
回答by orolo
I agree with Joe. For example, if it's a javascript app; json would be a strong candidate. Personally, I'd go with json for just about anything but only because that is the one I'm most comfortable with.
我同意乔。例如,如果它是一个 javascript 应用程序;json 将是一个强有力的候选人。就我个人而言,我会用 json 做任何事情,但只是因为这是我最舒服的一个。
回答by Alan H.
JSON would be my pick. JSON and YAML are lightweight and easy to get started with (no formal Schema required). JSON is more widely used and more compatible with various other technologies than YAML. For example, PHP has a built-in function to decode or encode JSON, not YAML. JavaScript of course just loves JSON, considering it's a strict subset of valid JavaScript.
JSON 将是我的选择。JSON 和 YAML 是轻量级的且易于上手(不需要正式的 Schema)。与 YAML 相比,JSON 的使用更广泛,并且与其他各种技术的兼容性也更好。例如,PHP 有一个内置函数来解码或编码 JSON,而不是 YAML。JavaScript 当然只是喜欢 JSON,因为它是有效 JavaScript 的严格子集。
回答by Joe
Depends on your needs. For small, lightweight apps I personally think XML is overkill: http://www.codinghorror.com/blog/2008/05/xml-the-angle-bracket-tax.html
取决于您的需求。对于小型轻量级应用程序,我个人认为 XML 是矫枉过正:http: //www.codinghorror.com/blog/2008/05/xml-the-angle-bracket-tax.html
I prefer YAML in that case. for interaction with javascript use json. If you truly need to define your own grammar (read: schema) then xml is it. Very powerful, you have to decide what you are trying to do - otherwise your question is too broad to give a definitive answer.
在这种情况下,我更喜欢 YAML。与 javascript 交互使用 json。如果您确实需要定义自己的语法(阅读:模式),那么 xml 就是它。非常强大,你必须决定你要做什么 - 否则你的问题太宽泛而无法给出明确的答案。
回答by steamer25
If the data's not hierarchical or going to have data interspersed in e.g., the description
This product is great for <targetDemo/> who love it's <featureSet/>), you may want to consider Comma Separated Values(CSV) or some other format like tab separated.
如果数据不是分层的或将数据散布在例如描述中
This product is great for <targetDemo/> who love it's <featureSet/>),您可能需要考虑逗号分隔值( CSV) 或其他一些格式,如制表符分隔。
It's old school but it gets the job done without weighing your file down with a bunch of describing text. I.e., in XML, you'd have the following non-value data for each feed.
这是老派,但它可以完成工作,而无需用一堆描述文本来衡量您的文件。即,在 XML 中,每个提要都有以下非值数据。
<feed name="" price="" type="" description=""/>
...contrasted with CSV:
...与 CSV 相比:
"", , "", ""
If you want, you can add header row at the top for documentation purposes.
如果需要,您可以在顶部添加标题行以用于文档目的。
There's also plenty of tooling around CSV, from command line utilities like awk to GUIs such as Excel.
还有很多围绕 CSV 的工具,从命令行实用程序(如 awk)到 GUI(如 Excel)。
Another alternative, if you don't really need the data to be editable via a text editor but don't want to deploy a more robust database service, would be SQLitewhich allows you to perform RDBMS-style CRUD operations on a flat binary file.
另一种选择,如果你真的不需要通过文本编辑器编辑数据,但不想部署更强大的数据库服务,那就是SQLite,它允许你在平面二进制文件上执行 RDBMS 风格的 CRUD 操作.
回答by Tom Anderson
In the absence of interoperability concerns, i don't think there's much in it. There are good libraries for all of them in all languages; some of them are built-in, some aren't. Yur interface to those libraries will be narrow - just in data-access code - so if one has a painful API, even that doesn't matter much.
在没有互操作性问题的情况下,我认为这没什么。所有语言都有很好的库;其中一些是内置的,有些不是。你对这些库的接口会很窄——只是在数据访问代码中——所以如果一个人有一个痛苦的 API,即使那也没什么大不了的。
JSON is, for me, the most pleasant to edit by hand, which is a small plus.
对我来说,JSON 是最适合手动编辑的,这是一个小加分项。
YAML can handle non-tree data structures using the &/* notation. Neither XML nor JSON have a built-in way to do that. Your use doesn't need it, though.
YAML 可以使用 &/* 表示法处理非树数据结构。XML 和 JSON 都没有内置的方法来做到这一点。但是,您的使用不需要它。
回答by wizztjh
I think xml is for big data and json is for small and not too complex data that do not need multiple dimension of array. I might be wrong. ^^ And i only see yaml in google app engine. Which appear to me , it is quite suitable for storing preferences and data of an application.
我认为 xml 适用于大数据,而 json 适用于不需要多维数组的小型且不太复杂的数据。我可能错了。^^ 而且我只在谷歌应用引擎中看到 yaml。在我看来,它非常适合存储应用程序的首选项和数据。

