html - 适用于结构化数据的文本友好文件格式

标签 html json xml yaml

我正在寻找一种文件格式,既可以对字典和数组等结构化数据进行编码,又可以轻松编辑文本 block ,包括换行符。

到目前为止的候选人:

  • xml:(+) 适合文本编辑和结构化数据,(-) 忽略换行符,结束标签很麻烦
  • html:(+) 有换行标记,(-) 无结构化数据
  • json:(+) 适合结构化数据,(-) 不适合编辑多行文本
  • yaml:(+) 适合结构化数据,(-) 如果文本包含冒号等特殊字符,则不适合编辑多行文本[编辑:查看接受的答案,文字样式有效]

到目前为止我最喜欢的是:带有自定义换行标记的 xml。更好的想法?

最佳答案

YAML 是一个完美的选择,你的“骗局”是“如果文本包含冒号等特殊字符,那么它不利于编辑多行文本”,这是完全没有根据的。 YAML 是迄今为止功能最丰富的多行文本格式:

---
# Block scalars are folded and stripped by default
preamble:
  We the People of the United States, in Order to form a more
  perfect Union, establish Justice, insure domestic Tranquility,
  provide for the common defence, promote the general Welfare,
  and secure the Blessings of Liberty to ourselves and our
  Posterity, do ordain and establish this Constitution for the
  United States of America.

# Chomping indicators (+ and -) allow explicit control over how
# leading/trailing whitespace will be preserved or stripped
chomp: >+


  Hello: Is it me you're looking for?



# Literal style preserves formatting
homepage: |
  <html>
    <head>
      <title>My kewl web site</title>
    </head>
    <body>
      <h1>Hello world!</h1>
    </body>
  </html>

# The indentation indicator lets you explicitly control indentation if it
# can't be inferred
indentation: |4

            I'll be indented eight spaces
          I'll be indented six

# And colons (or other special characters) are not a problem
emoji: |
  😀: Grinning face {U+1F600}
  😬: Grimacing face {U+1F62C}
  😞: Disappointed face {U+1F61E}

...当然,您可以在映射(字典)或序列(数组)中使用这些格式中的任何一种。您甚至可以使用复杂的字符串(或任何 YAML 结构 as it happens )来映射键。

如果您有一个您认为 YAML 不适合的用例示例,请随时发表评论。 YAML 并不完美,但它对很多事情都很有用。

为了进行比较,以下是 JSON 中的相同内容:

{ "preamble": "We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America.",
  "chomp": "\n\nHello: Is it me you're looking for?\n\n\n\n",
  "homepage": "<html>\n  <head>\n    <title>My kewl web site</title>\n  </head>\n  <body>\n    <h1>Hello world!</h1>\n  </body>\n</html>\n",
  "indentation": "\n        I'll be indented eight spaces\n      I'll be indented six\n",
  "emoji": "😀: Grinning face {U+1F600}\n😬: Grimacing face {U+1F62C}\n😞: Disappointed face {U+1F61E}\n"
}

关于html - 适用于结构化数据的文本友好文件格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34023526/

相关文章:

javascript - 使用提交按钮提交下拉框输入

php - PHP 中类似 Ruby 的数组参数实现

xml - 如何在 XSD 中定义一个简单的元素和属性

c# - 如何在不知道级别的情况下获取具有相同名称的所有 XML 节点?

Java tcp 套接字没有正确接收

python - 使用 ElementTree 删除父标签(不删除子标签)

javascript - 隐藏导航栏的滚动条并且仍然滚动

javascript - 将行动态添加到同一 HTML 中的 2 个不同表

javascript - 电子邮件无效 > 需要用户留在同一页面

c# - 如何处理反序列化变化的 JSON 数据?