我需要一种人类可读但可解析的文档格式 [英] I Need a Human Readable, Yet Parse-able Document Format

查看:141
本文介绍了我需要一种人类可读但可解析的文档格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在其中一个项目中,有百万种更好的方法来完成我需要的工作,但是我别无选择,我必须这样做.在这里:

I'm working on one of those projects where there are a million better ways to accomplish what I need but I have no choice and I have to do it this way. Here it is:

有一个Web表单,当用户填写表单并点击提交时,会使用表单数据创建人类可读的文本文件.看起来像这样:

There is a web form, when the user fills it out and hits a submit a human readable text file is created using the form data. It looks like this:

field_1: value for field one

field_2: value for field two
more data for field two (field two has a newline in it!)

field3: some more data

我的问题是:我需要将此文本文件解析回Web表单,以便用户可以对其进行编辑.

My problem is this: I need to parse this text file back into the web form so that the user can edit it.

我如何以一种万无一失的方式完成此任务?数据库不是一种选择,我必须使用这些文本文件.

How could I, in a foolproof way, accomplish this? A database is not an option, I have to use these text files.

我的问题:

  • 使用上面示例中的格式是否有万无一失的方法?
  • 哪种人类可读格式会更好(换句话说,我可以更改格式)
  • 可读性意味着非程序员可以阅读并知道什么.

该项目使用PHP.

更新

通过人类可读的方式,我的意思是任何人都可以阅读文本,而不会被它所淹没,包括您的祖母.

By human readable I mean that anyone could read the text and not be overwhelmed by it, including your grandmother.

推荐答案

我需要人类可读但 可解析文档格式

I Need a Human Readable, Yet Parse-able Document Format

这就是 YAML 的设计目标.您可以在他们的网站或 Wikipedia 上了解有关此内容的更多信息.

This is what YAML was designed to be. You can read more about it on their site or on Wikipedia.

引用维基百科:

YAML语法设计得很容易 映射到大多数通用的数据类型 高级语言:列表,哈希和 标量.熟悉的缩进 轮廓和苗条的外观使其成为现实 特别适合于以下任务 人类可能会查看或编辑数据 结构,例如配置 文件,调试过程中的转储,以及 文件标题

YAML syntax was designed to be easily mapped to data types common to most high-level languages: list, hash, and scalar. Its familiar indented outline and lean appearance makes it especially suited for tasks where humans are likely to view or edit data structures, such as configuration files, dumping during debugging, and document headers

与XML相比,优点是它不使用可能会使用户感到困惑的标签.而且我认为它比INI(也有人提到)更干净,因为它只使用冒号而不是等号,分号和引号.

The advantage over XML is that it doesn't use tags which might confuse users. And I think it's cleaner than INI (which was also mentioned) because it simply uses colons instead of equals signs, semicolons and quotes.

YAML示例如下:

invoice: 34843
date   : 2001-01-23
bill-to: &id001
    given  : Chris
    family : Dumars
    address:
        lines: |
            458 Walkman Dr.
            Suite #292
        city    : Royal Oak
        state   : MI
        postal  : 48046
ship-to: *id001
product:
    - sku         : BL394D
      quantity    : 4
      description : Basketball
      price       : 450.00
    - sku         : BL4438H
      quantity    : 1
      description : Super Hoop
      price       : 2392.00
tax  : 251.42
total: 4443.52
comments: >
    Late afternoon is best.
    Backup contact is Nancy
    Billsmer @ 338-4338.

这篇关于我需要一种人类可读但可解析的文档格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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