您知道哪些可用的 XML 语法替代方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51492/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:59:29  来源:igfitidea点击:

What usable alternatives to XML syntax do you know?

xmlsyntaxyaml

提问by aku

For me usablemeans that:

对我来说可用意味着:

  • it's being used in real-wold
  • it has tools support. (at least some simple editor)
  • it has human readable syntax (no angle brackets please)
  • 它正在真实世界中使用
  • 它有工具支持。(至少是一些简单的编辑器)
  • 它具有人类可读的语法(请不要使用尖括号)

Also I want it to be as close to XML as possible, i.e. there must be support for attributes as well as for properties. So, no YAMLplease. Currently, only one matching language comes to my mind - JSON. Do you know any other alternatives?

我还希望它尽可能接近 XML,即必须支持属性和属性。所以,请不要使用YAML。目前,我想到的只有一种匹配语言 - JSON。你知道其他选择吗?

回答by Ari

YAML is a 100% superset of JSON, so it doesn't make sense to reject YAML and then consider JSON instead. YAML does everything JSON does, but YAML gives so much more too (like references).

YAML 是 JSON 的 100% 超集,因此拒绝 YAML 然后考虑 JSON 是没有意义的。YAML 可以完成 JSON 所做的一切,但 YAML 也提供了更多(如引用)。

I can't think of anything XML can do that YAML can't, except to validate a document with a DTD, which in my experience has never been worth the overhead. But YAML is so much faster and easier to type and read than XML.

我想不出任何 XML 可以做而 YAML 不能做的事情,除了用 DTD 验证文档,根据我的经验,这从来不值得花费这些开销。但是 YAML 比 XML 更快、更容易输入和阅读。

As for attributes or properties, if you think about it, they don't truly "add" anything... it's just a notational shortcut to write something as an attribute of the node instead of putting it in its own child node. But if you like that convenience, you can often emulate it with YAML's inline lists/hashes. Eg:

至于属性或属性,如果您仔细考虑一下,它们并没有真正“添加”任何东西……这只是将某些东西写为节点的属性而不是将其放在自己的子节点中的符号快捷方式。但是如果您喜欢这种便利,您通常可以使用 YAML 的内联列表/哈希来模拟它。例如:

<!-- XML -->
<Director name="Spielberg">
    <Movies>
        <Movie title="Jaws" year="1975"/>
        <Movie title="E.T." year="1982"/>
    </Movies>
</Director>


# YAML
Director: 
    name: Spielberg
    Movies:
      - Movie: {title: E.T., year: 1975}
      - Movie: {title: Jaws, year: 1982}

For me, the luxury of not having to write each node tag twice, combined with the freedom from all the angle-bracket litter makes YAML a preferred choice. I also actually like the lack of formal tag attributes, as that always seemed to me like a gray area of XML that needlessly introduced two sets of syntax (both when writing and traversing) for essentially the same concept. YAML does away with that confusion altogether.

对我来说,不必为每个节点标记写两次的奢侈,加上从所有尖括号垃圾中解放出来,使 YAML 成为首选。我实际上也喜欢缺少正式的标签属性,因为在我看来,这总是像 XML 的灰色区域,它不必要地为基本相同的概念引入了两组语法(在编写和遍历时)。YAML 完全消除了这种混淆。

回答by Staale

JSONis a very good alternative, and there are tools for it in multiple languages. And it's really easy to use in web clients, as it is native javascript.

JSON是一个非常好的替代方案,并且有多种语言的工具。而且它在 Web 客户端中非常容易使用,因为它是原生 javascript。

回答by wvxvw

TL;DR

TL; 博士

Prolog wasn't mentioned here, but it is the best format I know of for representing data. Prolog programs, essentially, describe databases, with complex relationships between entities. Prolog is dead-simple to parse, whose probably only rival is S-expressions in this domain.

此处未提及 Prolog,但它是我所知道的用于表示数据的最佳格式。Prolog 程序本质上是描述数据库,在实体之间具有复杂的关系。Prolog 解析起来非常简单,它可能唯一的竞争对手是该领域中的 S 表达式。

Full version

完整版本

Programmers often "forget" what XML actually consists of. Usually referring to a very small subset of what it is. XML is a very complex format, with at least these parts: DTD schema language, XSD schema language, XSLT transformation language, RNG schema languageand XPath(plus XQuery) languages - they all are part and parcel of XML standard. Plus, there are some apocrypha like E4X. Each and every one of them having their own versions, quite a bit of overlap, incompatibilities etc. Very few XML parsers in the wild implement all of them. Not to mention the multiple quirks and bugs of the popular parses, some leading to notable security issues like https://en.wikipedia.org/wiki/XML_external_entity_attack.

程序员经常“忘记”XML 实际上是由什么组成的。通常指的是它是什么的一个非常小的子集。XML 是一种非常复杂的格式,至少有以下几个部分:DTD 模式语言XSD 模式语言XSLT 转换语言RNG 模式语言XPath(加上 XQuery)语言——它们都是 XML 标准的重要组成部分。另外,还有一些像E4X这样的伪经。它们中的每一个都有自己的版本,相当多的重叠、不兼容等。在野外很少有 XML 解析器实现所有这些。更不用说流行解析的多个怪癖和错误,其中一些会导致显着的安全问题,例如https://en.wikipedia.org/wiki/XML_external_entity_attack

Therefore, looking for an XML alternativeis not a very good idea. You probably don't want to deal with the likes of XML at all.

因此,寻找 XML替代方案并不是一个好主意。您可能根本不想处理 XML 之类的问题。

YAML is, probably, the second worst option. It's not as big as XML, but it was also designed in an attempt to cover all bases... more than ten times each... in different and unique ways nobody could ever conceive of. I'm yet to hear about a properly working YAML parser. Ruby, the language that uses YAML a lot, had famously screwed upbecause of it. All YAML parsers I've seen to date are copies of libyaml, which is itself a hand-written (not a generated from a formal description) kind of parser, with a code which is very difficult to verify for correctness (functions that span hundreds of lines with convoluted control flow). As was already mentioned, it completely contains JSON in it... on top of a handful of Unicode coding techniques... inside the same document, and probably a bunch of other stuff you don't want to hear about.

YAML 可能是第二糟糕的选择。它不像 XML 那样大,但它的设计也试图涵盖所有基础……每个都超过十次……以任何人都无法想象的不同和独特的方式。我还没有听说过一个正常工作的 YAML 解析器。Ruby 是一种经常使用 YAML 的语言,因此出了名的搞砸了。迄今为止我见过的所有 YAML 解析器都是libyaml 的副本,它本身是一种手写(不是从正式描述生成)类型的解析器,其代码的正确性很难验证(跨越数百行的复杂控制流的函数)。正如已经提到的,它完全包含 JSON ......在少数 Unicode 编码技术之上......在同一个文档中,可能还有一堆你不想听到的其他东西。

JSON, on the other hand, is completely unlike the other two. You can probably write a JSON parser while waiting for downloading JSON parser artefact from your Maven Nexus. It can do very little, but at least you know what it's capable of. No surprises. (Except some discrepancies related to character escaping in strings and doubles encoding). No covert exploits. You cannot write comments in it. Multiline strings look bad. Whatever you mean by distinction between properties and attributes you can implement by more nested dictionaries.

另一方面,JSON 与其他两个完全不同。您可以在等待从 Maven Nexus 下载 JSON 解析器工件的同时编写 JSON 解析器。它可以做的很少,但至少你知道它的能力。没有惊喜。(除了一些与字符串和双精度编码中的字符转义相关的差异)。没有秘密攻击。你不能在里面写评论。多行字符串看起来很糟糕。无论你说的属性和属性之间的区别是什么,你都可以通过更多的嵌套字典来实现。

Suppose, though you wanted to right what XML wronged... well, then the popular stuff like YAML or JSON won't do it. Somehow fashion and rational thinking parted ways in programming some time in the mid seventies. So, you'll have to go back to where it all began with McCarthy, Hoare, Codd and Kowalski, figure out what is it you are trying to represent, and then see what's the best representation technique there is for whatever is that you are trying to represent :)

假设,虽然您想纠正 XML 错误的地方……好吧,那么像 YAML 或 JSON 这样流行的东西不会这样做。不知何故,时尚和理性思维在七十年代中期的某个时候在编程中分道扬镳。所以,你必须回到一切从麦卡锡、霍尔、科德和科瓦尔斯基开始的地方,弄清楚你想要表达的是什么,然后看看什么是最好的表达技术,无论你是什么试图代表:)

回答by Ferruccio

I have found S-Expressionsto be a great way to represent structured data. It's a very simple format which is easy to generate and parse. It doesn't support attributes, but like YAML & JSON, it doesn't need to. Attributes are simply a way for XML to limit verbosity. Simpler, cleaner formats just don't need them.

我发现S-Expressions是表示结构化数据的好方法。这是一种非常简单的格式,易于生成和解析。它不支持属性,但像 YAML 和 JSON 一样,它不需要。属性只是 XML 限制冗长的一种方式。更简单、更干净的格式只是不需要它们。

回答by Giovanni Galbo

Jeff wrote about this hereand here. That should help you get started.

杰夫在这里这里写了这个。这应该可以帮助您入门。

回答by dbr

You're demands are a bit impossible.. You want something close to XML, but reject probably the closest equivalent that doesn't have angle-bracket (YAML).

你的要求有点不可能......你想要接近 XML 的东西,但可能拒绝最接近的没有尖括号 (YAML) 的等价物。

As much as I dislike it, why not just use XML? You shouldn't ever have to actually read XML (aside from debugging, I suppose), there are an absurd amount of tools about for it.

尽管我不喜欢它,为什么不直接使用 XML?你不应该真正阅读 XML(除了调试,我想),有很多关于它的工具。

Pretty much anything that isn't XML isn't going to be as widely used, thus there will be less tool support.

几乎所有不是 XML 的东西都不会被广泛使用,因此工具支持会减少。

JSON is probably about equivalent, but it's pretty much equally unreadable.. but again, you shouldn't ever have to actually read it (load it into whatever language you are using, and it should be transformed into native arrays/dicts/variables/whatever).

JSON 可能是等价的,但它几乎同样不可读......但同样,你不应该真正阅读它(将它加载到你正在使用的任何语言中,并且它应该被转换为本机数组/dicts/variables/任何)。

Oh, I do find JSON farnicer to parse than XML: I've used it in Javascript, and the simplejson Python module - about one command and it's nicely transformed into a native Python dict, or a Javascript object (thus the name!)

哦,我确实发现JSON更好比XML解析:我已经用它在Javascript和simplejson Python模块-大约一个命令和它很好地转化成原来的Python字典,或者Javascript对象(因此名字!)

回答by Pat

I would recommend JSON ... but since you already mentioned it maybe you should take a look at Google protocol buffers.

我会推荐 JSON ...但既然你已经提到了它,也许你应该看看Google protocol buffers

Edit: Protocol buffers are made to be used programatically (there are bindings for c++, java, python ...) so they may not be suited for your purpose.

编辑:协议缓冲区以编程方式使用(有 c++、java、python 的绑定......),因此它们可能不适合您的目的。

回答by intellimath

There is AXONthat cover the best of XML and JSON. Let's explain that in several examples.

AXON覆盖最好的XML和JSON的。让我们用几个例子来解释一下。

AXON could be considered as shorter form of XML data.

AXON 可以被视为 XML 数据的较短形式。

XML

XML

<person>
   <name>Frank Martin</name>
   <age>32</age>
 </person>

AXON

轴心

person{
  name{"Frank Martin"}
  age{32}}

or

或者

person
  name:
    "Frank Martin"
  age:
    32

XML

XML

<person name="Frank Martin" age="32" />

AXON

轴心

person{name:"Frank Martin" age:32}

or

或者

person
  name: "Frank Martin"
  age: 32

AXON contains some form of JSON.

AXON 包含某种形式的 JSON。

JSON

JSON

{"name":"Frank Martin" "age":32 "birth":"1965-12-24"}

AXON

轴心

{name:"Frank Martin" age:32 birth:1965-12-24}

AXON can represent combination of XML-like and JSON-like data.

AXON 可以表示类 XML 和类 JSON 数据的组合。

AXON

轴心

table {
  fields {
    ("id" "int") ("val1" "double") ("val2" "int") ("val3" "double")
  }
  rows {
    (1 3.2 123 -3.4)
    (2 3.5 303 2.4)
    (3 2.3 235 -1.2)
  }
}

or

或者

table
  fields
    ("id" "int")
    ("val1" "double")
    ("val2" "int") 
    ("val3" "double")
  rows
    (1 3.2 123 -3.4)
    (2 3.5 303 2.4)
    (3 2.3 235 -1.2)

There is available the python library pyaxonnow.

现在有可用的 python 库pyaxon

回答by Qwertie

For storing code-like data, LES(Loyc Expression Syntax) is a budding alternative. I've noticed a lot of people use XML for code-like constructs, such as build systems which support conditionals, command invocations, sometimes even loops. These sorts of things look natural in LES:

对于存储类似代码的数据,LES(Loyc 表达式语法)是一种新兴的替代方法。我注意到很多人将 XML 用于类似代码的构造,例如支持条件、命令调用,有时甚至是循环的构建系统。这些事情在 LES 中看起来很自然:

// LES code has no built-in meaning. This just shows what it looks like.
[DelayedWrite]
Output(
    if version > 4.0 {
        $ProjectDir/Src/Foo;
    } else {
        $ProjectDir/Foo;
    }
);

It doesn't have great tool support yet, though; currently the only LES library is for C#. Currently only one app is known to use LES: LLLPG. It supports "attributes" but they are like C# attributes or Java annotations, not XML attributes.

不过,它还没有很好的工具支持;目前唯一的 LES 库是用于 C# 的。目前已知只有一个应用程序使用 LES:LLLPG。它支持“属性”,但它们类似于 C# 属性或 Java 注释,而不是 XML 属性。

In theory you could use LES for data or markup, but there are no standards for how to do that:

理论上,您可以将 LES 用于数据或标记,但没有关于如何做到这一点的标准:

body {
    '''Click here to use the World's '''
    a href="http://google.com" {
        strong "most popular"; " search engine!"
    };
};

point = (2, -3);
tasteMap = { "lemon" -> sour; "sugar" -> sweet; "grape" -> yummy };

回答by epatel

I think Clearsilveris a very good alternative. They even have a comparison page hereand a list of projectsthat use it

我认为Clearsilver是一个非常好的选择。他们甚至在这里有一个比较页面和使用它的项目列表