xml
  1. xml-comments

XML Comments

  • XML allows the addition of comments in the code.
  • Comments are used to add explanatory notes or to disable a certain portion of the code.

Syntax

<!-- This is a comment -->

Example

<root>
  <employee>
    <name>John</name>
    <salary>50000</salary>
    <!-- This employee is a part-time worker -->
  </employee>
  <employee>
    <name>Mike</name>
    <salary>75000</salary>
  </employee>
  <!-- Total number of employees: 2 -->
</root>

Output

The output of the example XML code will be:

<root>
  <employee>
    <name>John</name>
    <salary>50000</salary>
  </employee>
  <employee>
    <name>Mike</name>
    <salary>75000</salary>
  </employee>
</root>

Explanation

In the above example, a comment is added to denote that an employee is a part-time worker. Another comment is added after the end of the <employee> element to denote the total number of employees present in the XML code.

Use

The comments are used to add explanatory notes in the XML code. It is also used to disable a certain portion of the code without deleting it.

Important Points

  • Comments in XML must start with <!-- and end with -->.
  • Comments cannot be nested.
  • Comments cannot contain the string --.

Summary

In this article, we covered the syntax, example, output, explanation, use, important points and summary of comments in XML. Comments are used to add explanatory notes or to disable a certain portion of the XML code.

Published on: