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

后端 未结 5 1380
猫巷女王i
猫巷女王i 2021-02-05 08:50

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:

Ther

相关标签:
5条回答
  • 2021-02-05 09:25

    You might want to look into YAML

    http://www.yaml.org/

    I agree with Pablo Fernandez response. I think JSON might be a good choice as well.

    0 讨论(0)
  • 2021-02-05 09:28

    XML is an option.

    0 讨论(0)
  • 2021-02-05 09:29

    I'm just gonna say that an INI string is pretty readable:

    Pet_Name = "Fred"
    

    But, you could always roll your own format. Something like:

    Key: ValueValueValueValueValueValue
    Key: ValueValue
    

    Basically, you would explode the string by newlines, look for text strings infront of colons and use that as the key, and the data after the colon and before the newline is the value.

    0 讨论(0)
  • 2021-02-05 09:32

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

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

    To quote Wikipedia:

    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

    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.

    Sample YAML looks like:

    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.
    
    0 讨论(0)
  • 2021-02-05 09:39

    I'd say either use

    • INI files or
    • YAML or
    • Markdown or
    • Textile

    or just about any lightweight markup language you deem appropriate.

    0 讨论(0)
提交回复
热议问题