You might have already heard about the two main languages, i.e. XML and YAML. XML is an eXtensible Markup Language while Yaml is a human-readable data-serialization language. Let us look at the comparison of both languages in terms of their file structure, syntax and usage.
What is YAML?
YAML stands for Yet Another Markup Language or YAML Ain’t Markup Language (a recursive acronym), which is a data serialization language. It strongly emphasizes on storing and transmitting data only but not the documents. It can support the same applications as XML does and can work with other languages too. It is human-readable and easy to learn.
What is XML?
XML stands for Extensible Markup Language, which has a set of codes and rules that describe the format of a text in an XML document. It is a simple text-based programming language to represent structured information like data, documents, configurations, and much more.
YAML Syntax
YAML features derives from multiple languages like XML, HTML, C, and others. The JSON format is a subset of YAML language; therefore, JSON files are valid in YAML language. It has nested indentation as Python. You can use white spaces but tab characters are not allowed. It uses both .yml or .yaml file extensions.
XML Syntax
XML has certain set of rules that needs to be followed in order to have a proper structured document. All the opening tags must have a closing tag, which is case-sensitive. There must be a root element in the xml document.
YAML Structure
The basic structure of a YAML file is either a map or list.
Map in YAML
The map structure allows you to have key-pair values. Each key must be unique and can be in any order. Before creating a new map, the previous map must be closed or increase the indentation level of a map.
key: value |
List in YAML
The other type of a yaml document is a list. The values of a list can be listed in a specific order and can have any number of items. It starts with a dash ‘-‘ followed by a space. Indentation is used to separate the list from its parent object. It can also be embedded in a map.
Scalar Values in YAML
The yaml document can also have a scalar value, which is encoded in Unicode format such as integers, dates, strings, numbers, and booleans.
XML Structure
An XML document must consist of three parts.
- XML Declaration
- Document Type Declaration
- Document Type Body
XML Declaration
The first part of an XML document is an XML declaration. It is technically optional but is recommended to add the declaration in the top of a document. It identifies the document as a XML document. It can also indicate whether the declaration can refer external Document Type Definition (DTD).
You can give the following declaration before starting the XML document
<?xml version=”1.0″ ?> |
There must be no space before the xml keyword and between the angular brackets. It is a case sensitive declaration; therefore, all the keywords must be in lowercase. If the xml document has any associated with external DTDs, then the correct declaration must be followed as given below:
<?xml version=”1.0″ standalone=”no” ?> |
Document Type Declaration
After giving xml declaration, document type declaration must be given to declare the root element. It is also called as a document element and it provides the location of DTD. You can declare the document type as given below:
<!DOCTYPE RootElement (SYSTEM | PUBLIC)
ExternalDeclarations? [InternalDeclarations]? > |
Document Body
Once the xml and document type are declared, the main content can be defined called as Document Body. It contains the real data with respect to the different instances of an application. You can take an example of an HTML page.
What is YAML used for?
The main purpose of yaml file is to serve as a configuration file in an application as it is recommended to write the configurations of an application in YAML instead of JSON or XML as it is more flexible with good readability. A lot of automation tools like Ansible use it as part of its configuration and creation of processes. It can also be used to track the changes inside source control.
What is XML used for?
XML is used to describe the data. It separates the data from the presentation. It can be used in developing the HTML applications since HTML is used to format the data and XML can be used to send or receive the data. It can also be used with the conjunction of JavaScript language to read the data.