XML file and text files! [closed]
I want to know the advantages of using an XML file over a text file?please help me thanks!
XML means a structured document, standard Validation and description tools (DTD, Schema), and a standardized way of parsing them (i.e. DOM)
"Raw" text files might mean easier to write (no need to respect any tag imbrication or anything), but as they don't necessarily have a well-defined structure, they can get harder to parse.
There many advantages in general. It depends what is your case and what of them are applicable to you. To start, the txt file contains only data but there are no indicators what this data mean. Also, the structure of the data in txt is fixed and it's not documented in the txt itself.
With XML you have lots of additional technologies you can use - XSD for validating the structure of the XML document - to check whether it's correct and fulfill your requirements. Also, you've got XSL (and XSLT with it) for transforming this document in desired shape and format. And of course the XPath for searching, selecting and extracting data from your XML document.
Of course, the obvious disadvantage of XML is the bigger size, for the same amount of data. So, it's a trade off here. You should consider whether this trade off is useful in your case.
It all depends on the context, but in general:
Advantages:
- Most languages come with an xml parser, which makes it easy to load and parse the data
- XMLs hierarchical nature maps well to an object hierarchy, therefore it's easy to map from one to the other
- It's application agnostic.
- There are a variety of bolt-on technologies, such as XSLT, XPath and XQuery that allow you to manipulate the data.
Disadvantages:
- It's verbose and may lead to unnecessary noise in your data.
- It's not compact, which can be an issue if you are transmitting the data
- Parsing can be a lot slower that just reading a line from a CSV file (for example) and splitting it based on a separator.
XML is also a text file, but with a well-defined structure which provides ways to represent complicated data structures. Though the flexibility of XML has a cost of verbosity and higher processing costs.
When data structures are simple (e.g. list of strings, or simple name-value pairs) then usually a simple text file fits better. Also, when the file is supposed to be used only by one application or system, then it often make sense to choose other than XML text file format, as it can be simpler to handle.
XML is usually a good choice when different applications/system interact and the data being exchanged has complicated (but preferably well defined) structure. The choice depends on the specific problem. You cannot say 'XML is always better that simple text file', nor the opposite.
The very short answer: you won't have to build your own parser, you can use an XML parser already present for your platform.
Though be considerate: for two-line config files and the like, using XML would hardly pay off. Also consider your other options:
- Windows-style INI files (in Python, there is a parser for that: http://docs.python.org/library/configparser.html)
- YAML
just to name a few.
The following are the advantages of xml over text file
- Parsing it is a easy task because it has a well defined structure
- Can define schema for the xml and there by you can traverse through the xml pretty easily and it becomes type safe.
- Better performance.
- More reliable.
精彩评论