18

诚然,XML 非常有用,但可能非常冗长。有哪些替代品,它们是否专门用于任何特定目的?轻松查询内容的库支持是一大优势。

4

24 回答 24

18

There seems to be a lot of multi-platform support for JSON.

于 2008-09-04T17:16:36.997 回答
13

Jeff's article on The Angle Bracket Tax summarizes a number of alternatives (well, mainly YAML), and led me to the wiki article on lightweight markup languages.

Update: Although YAML is a possible "alternative to XML" for some applications, the two are not, as I first thought, isomorphic.

Indeed, it "ain't markup language."

Furthermore, YAML ain't as "lightweight" as it appears. For documents that can be represented in plain XML (such as Jeff's example), YAML is clearly less verbose. But YAML offers many other specialized structures, enlisting many more characters and sequences than are reserved by XML.

Bottom line, if you're looking for XML-without-angle-brackets, YAML ain't it.

于 2008-09-04T17:26:56.863 回答
9

Don't forget about YAML!

JSON seems to have better support though. For example, the Prototype JS library has excellent built-in JSON functions.

于 2008-09-04T17:17:50.753 回答
5

I wouldn't dismiss plain text, like CSV or tab-delimited.

于 2008-09-04T17:28:52.210 回答
4

HDF5是一种非常紧凑的数据格式,具有一些类似于 xml 的特性。.net 库还有很多不足之处,但该格式在大小和性能方面都可以很好地扩展。

于 2008-09-09T18:32:52.277 回答
4

我使用 XML 的工作几乎完全是使用以文档为中心的 XML,它必须对任意嵌套结构的长序列进行建模。我还没有使用过 JSON,但我的印象是,它与类文档数据一起使用很麻烦,但与类记录数据一起使用时适应良好甚至优雅。在做出决定时考虑数据的形状。

于 2008-11-11T15:41:56.013 回答
4

You could try google's protobufs. It's much faster than XML. There are libraries for it in C, C++, C#, Java and Python (there are alpha versons for ruby and perl). But it is binary.

于 2008-11-13T22:47:00.703 回答
2

S-Expressions work great if you don't need to apply attributes to elements. Another alternative is YAML.

于 2008-09-04T17:17:10.270 回答
2

XML 经常用于配置,在这种情况下,还有一些其他经常使用的简单存储格式(较少面向文档):

  1. .property 文件
  2. INI 文件

根据平台和语言的不同,有多种读写方式。

于 2008-09-04T19:13:29.320 回答
2

你想对数据做什么?存储吗?传过去?显示吗?这些问题应该会推动您寻找合适的技术。简单地问你应该如何格式化你的数据就像问你应该用什么语言来编程,而不指定你想要完成什么。

对于大多数数据任务,Codd 博士可以解决问题:http ://en.wikipedia.org/wiki/Edgar_F._Codd 。数据库应该能够做任何你想做的事情。

如果您要传递它,我提倡纯文本。当您滚动自己的二进制格式时,您的数据会随着解析器的消失而消失。

对于纯文本,更深层次的问题是在哪里放置元数据。它应该在数据文件的外部还是内部(“自我描述”)。

例如,XML 是纯文本,但源代码也是。有了源文件,就有一个关于语法和语义的详细说明,而 XML应该是自描述的。问题是它不是。此外,它是从文档表示和标记演变而来的,但现在被滥用于各种数据序列化、传输和存储。

于 2008-09-04T19:36:48.877 回答
2

如果有人在寻找与 XML 或多或少同构的 XML 的不那么冗长的替代方案,那么就有AXON。为了解释考虑 XML 和 AXON 中等效表示的示例。还有支持 AXON 格式的 python 库pyaxon

XML

<person>
   <name>Alex</name>
   <age>34</age>
   <email>mail@example.com</email>
</person>

轴突

person {
  name {"Alex"}
  age {34}
  email {"mail@example.com"}}

XML

<memo date="2008-02-14">
<from>
<name>The Whole World</name><email>us@world.org</email>
</from>
<to>
<name>Dawg</name><email>dawg158@aol.com</email>
</to>
<message>
Dear sir, you won the internet. http://is.gd/fh0
</message>
</memo>

轴突

memo {
  date:2008-02-14
  from {
    name{"The Whole World"} email{"us@world.org"}}
  to {
    name{"Dawg"} email{"dawg158@aol.com"}}
  message {"Dear sir, you won the internet. http://is.gd/fh0"}
}

XML

<club>
  <players>
    <player id="kramnik"
       name="Vladimir Kramnik"
       rating="2700"
       status="GM" />
    <player id="fritz"
       name="Deep Fritz"
       rating="2700"
       status="Computer" />
    <player id="mertz"
      name="David Mertz"
      rating="1400"
      status="Amateur" />
  </players>
  <matches>
    <match>
      <Date>2002-10-04</Date>
      <White refid="fritz" />
      <Black refid="kramnik" />
      <Result>Draw</Result>
    </match>
    <match>
      <Date>2002-10-06</Date>
      <White refid="kramnik" />
      <Black refid="fritz" />
      <Result>White</Result>
    </match>
  </matches>
</club>

轴突

club {
  players {
    player {
      id:"kramnik"
      name:"Vladimir Kramnik"
      rating:2700
      status:"GM"}
    player {
      id:"fritz"
      name:"Deep Fritz"
      rating:2700
      status:"Computer"}
    player {
      id:"mertz"
      name:"David Mertz"
      rating:1400 
      status:"Amateur"}}
  matches {
    match {
     Date{2002-10-04}
     White{refid:"fritz"}
     Black{refid:"kramnik"}
     Result{"Draw"}}
    match {
      Date{2002-10-06}
      White{refid:"kramnik"}
      Black{refid:"fritz"}
      Result{"White"}}}}
于 2015-04-29T14:35:38.433 回答
2

TOML是新的大事。它具有 YAML 的优点,但没有大规格。它扩展了一种常见且熟悉的配置文件格式。它直接类似于(并可翻译成)JSON。支持所有大语言。由 Github 联合创始人/总裁 Tom 创建并自恋地命名。这很棒。试一试!

示例 TOML:

# This is a TOML document. Boom.

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
organization = "GitHub"
bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
dob = 1979-05-27T07:32:00Z # First class dates? Why not?

[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true

[servers]

  # You can indent as you please. Tabs or spaces. TOML don't care.
  [servers.alpha]
  ip = "10.0.0.1"
  dc = "eqdc10"

  [servers.beta]
  ip = "10.0.0.2"
  dc = "eqdc10"

[clients]
data = [ ["gamma", "delta"], [1, 2] ]

# Line breaks are OK when inside arrays
hosts = [
  "alpha",
  "omega"
]
于 2014-03-07T01:40:57.937 回答
1

For the sake of completeness I will mention Edifact for which I wrote an interface a long time ago.

于 2008-09-04T17:30:52.843 回答
1

I wouldn't dismiss plain text, like CSV or tab-delimited.

I'm really looking for alternatives that have a defined structure and (cross platform, multi language) library support. I'm interested in looking at different designs and their pros and cons. I like the idea of formats that can have a text and "binary" (compact, "compiled", fast I/O, smaller footprint) format. The advantage of having libraries is that they perform the parsing and perhaps extra data manipulation/validation for you.

Although having said that, there is definitely a use for simple formats like .ini, .plist and CSV etc. You shouldn't always have to use a hammer to crack a nut.

于 2008-09-10T09:22:48.487 回答
1

JSON 是有效的 YAML,它可能非常有用。两人合一!

于 2008-09-04T18:52:57.013 回答
1

异端!XML 是数据之王。对篡位者说不,砍掉他们的脑袋!XML 万岁!

但是如果只需要数据使用 Json 来获得支持和优雅,但如果你需要格式化,xpath 之类的查询,附加元数据等......坚持使用 XML

注意:我使用 Xml 配置系统构建代码生成和类似任务,但 Json 用于 Rpc,Sql 用于查询和持久性,最后 Yaml 在这里和那里用于日志记录和快速任务,换句话说,根据需要选择合适的格式。

于 2008-11-11T16:10:57.423 回答
1

但代价是什么?

在许多情况下,我都支持 JSON,尤其是在关注重量或客户端工作的情况下,但远离 XML 会失去可读性(在那些配置文件中非常重要)以及未来问题解决方案(如 XSLT 和 XPath)的力量。真正确定为什么以及何时离开:这是一个事实上的标准是有原因的。

(旁白:我的习惯是在内部使用 XML,然后将其转换为 JSON,这是所需的输出)

于 2008-11-11T09:18:39.673 回答
1

简单声明式语言是用于序列化和配置等常见任务的 XML 的一个很好的替代方案。它提供了一个 C# 和 Java 解析器库。我认为它擅长在没有 XML 冗长的情况下指定各种元数据。

于 2011-04-13T01:13:37.593 回答
0

为了提及......看看我的建议:

http://igagis.github.io/stob/

它非常简单,没有各种特殊符号的重载,基本上只有 {} 和 ""。

支持 C++ 风格的注释。

有 C++、C# 和 Java 库。

例子:

"String object"
AnotherStringObject
"String with children"{
    "child 1"
    Child2
    "child three"{
        SubChild1
        "Subchild two"

        Property1 {Value1}
        "Property two" {"Value 2"}
        //comment

        /* multi-line
           comment */

        "multi-line
         string"

        "Escape sequences \" \n \r \t \\"
    }
}
于 2014-12-15T15:16:20.397 回答
0

如果您从 DSL 的角度提问,Guile Scheme可能会有所帮助,正如 S 表达式中已经建议的那样。

我个人也将 JSON 用于 AJAX 事务。

于 2008-09-04T20:51:54.013 回答
0

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

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

不过,它还没有很好的工具支持。目前唯一的 LES 库是用于 C# 的。目前已知只有一个应用程序使用 LES:LLLPG

从理论上讲,您可以将 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 };
于 2013-12-03T22:55:18.637 回答
0

任何你喜欢的,只要不是 ASN.1

于 2011-01-20T17:49:14.507 回答
0

XML 可以用于文本标记,但对于一般结构,序列化是一个非常糟糕的选择,JSON 更适合。

于 2010-12-05T22:59:39.160 回答
0

JSON 可以以多种方式使用,但它特别适合与我发现的 MySQL 表一起使用。它也适用于 Android(GSON 库或 JSON)。除此之外,它还可以有效地单独或作为数组传输少量数据。

于 2012-04-28T00:41:22.813 回答