XML vs comma delimited text files

后端 未结 12 2691
面向向阳花
面向向阳花 2021-02-20 08:25

Ok, I\'ve read a couple books on XML and wrote programs to spit it out and what not. But here\'s the question. Both a comma delimited file and a XML file are \"human readable.

相关标签:
12条回答
  • 2021-02-20 08:57

    Well XML is human readable and human editable. You can look at an XML file and know exactly what it is. A CSV file is human readable but you don't really know what each value means at all.

    For example, if we're storing user accounts, which would you prefer?

    <user>
        <username>ryeguy</username>
        <password>abc123</password>
        <regdate>3-4-08</regdate>
        <email>my@email.com</email>
    </user>
    

    OR

    ryeguy,abc123,3-4-08,my@email.com
    

    Of course, this is just an example, but imagine it with 30 fields or so!

    Or worse yet, what if we make subfields?

    <user>
        <username>ryeguy</username>
        <password>abc123</password>
        <regdate>3-4-08</regdate>
        <email>my@email.com</email>
        <posts>
            <post>
                <id>34</id>
                ....
            </post>
        </posts>
    </user>
    

    That would be a pain in the ass to put in a CSV. Soon you'd be making your own querying language.

    0 讨论(0)
  • 2021-02-20 08:57

    Xml can be validated against a contract (schema or DTD).

    0 讨论(0)
  • 2021-02-20 08:59

    CSV was never really a standard. Just the same quick and dirty method a bunch of people came up with independently. Of course, some of these people were smarter than others and realized you needed to escape characters but others didn't. Even MSSQL exports CSVs improperly. There is a documented RIGHT way to doing XML so if you're doing it right and someone's application or whatever isn't accepting it you have some clout when you say "That's not my fault."

    0 讨论(0)
  • 2021-02-20 09:01

    XML supports complex, structured and hierarchical representation of things. That's far from what CSV can store trivially.

    Think about a complex object graph in an object oriented environment. It can be serialized as an XML document pretty easily but CSV cannot handle such a thing.

    0 讨论(0)
  • 2021-02-20 09:02

    These aren't the only two options, you can also use JSON or YAML which are much lighter weight than xml.

    In general, if you have simple tabular data with out many special characters, CSV isn't a bad choice. For structured data, consider using one of the other 3.

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

    Among the reasons you may prefer XML over CSV (depends on the task at hand of course): * Almost all platforms and languages have existing libraries for reading, writing, parsing, and manipulating XML. * XML has well-defined rules for encoding all characters. CSV has ambiguities such as how to encode commas that are part of the data. * XML supports a variety of data shapes (like hierarchical) where as CSV is most useful when the data looks like a table (rows and columns).

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