EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials XML Tutorial XML Comments
Secondary Sidebar
XML Tutorial
  • Basic
    • What is XML?
    • XML Tags
    • XML URL
    • XPath Sibling
    • XML root element
    • XML Encryption
    • XML Parsing php
    • xml parsing with java
    • Dataset XML
    • XML Parser in C#
    • XML Tree
    • XML boolean
    • XML sitemap
    • XML Array
    • XML reserved characters
    • XML Viewer
    • XML Uses
    • XML Special Characters
    • XML generator
    • XML file format
    • XML DOM
    • XML ampersand
    • XML Mapping
    • XML File
    • XML Element
    • XML HttpRequest
    • XML XSD
    • XML Schema
    • XML Namespaces
    • XML Comments
    • XML Attributes
    • XML Encoding
    • XML Validation
    • XML CDATA
    • XML Database
    • XML Technologies
    • XML Error
    • XML DTD
    • XML Date
    • XML Parsers
    • String in XML
    • XML with CSS
    • XML Versions
    • XML Features
    • XML Commands
    • Servlet web.xml
    • XPath Injection
    • XPath Functions
    • XPath starts-with
    • XPath Selector
    • XPath Count
    • XPath XML
    • XML Parsing in Oracle
    • XML parsing in python
  • Xpath
    • What is XPath?
    • XPath namespace
    • XPath for JSON
    • XPath Last Element
    • Xpath Substring
    • XPath First Element
    • XPath local-name
    • XPath Multiple Attributes
    • XPath Matches
    • XPath Position
    • XPath Index
    • XPath regex
    • XPath id contains
    • XPath innertext
    • XPath Multiple Conditions
    • XPath Helper
    • XPath Generator
    • XPath ID
    • XPath Locator

XML Comments

By Priya PedamkarPriya Pedamkar

XML Comments

Introduction to XML Comments

XML Comments could be defined by two parts, At first XML like HTML is a mark-up language, and you can create the own tags and the comments in the programming language, they are some small piece of text, formulas or any expressions included in the XML source code stating programming instructions on each line and can be added anywhere in the XML code which benefits the developer to understand the existing code for future analysis. The Comments are completely ignored by the compiler and help the users in the future while reading the code. So, XML Comments is a single character or a statement of paragraphs that are apart from code provides formal documentation and helps in understanding the common tags used in the XML file. It helps in including notes or any observations to be included in XML File.

Types of XML Comments

Comments in XML is declared as:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,950 ratings)

It has two sections starting and ending the comment. Have to be very clear that nesting comments are not done. here is the syntax.

<!  - -Hello this is comment - - >

Note: dashes are not allowed to place in the middle of the comments. Both sides include two dashes. XML comment supports all HTML tags to add additional usages.

The tree structure form is given as:

Comment:

<driver>< ! – MIO  taxi Truck Driver - -></driver>

tree structure

Illegal representation of Xml Comment is shown below:

<!—Menu in the restaurant --
Type of dish -->            // No character sequence allowed.
<!—Calculator example --->  // illegal end of hypen
<!-- xxxxxxx<!-- yyyyyy--> -->  // cannot be nested

XML Comments & Examples

XML Comments can be categorized and used in the following ways.

1. Comments Inside an XML Document

This is the usual type where a comment is placed at the top. The need for comment potentially makes the code clear also whenever a document code is updated after a year or a month later it is required to update comment too.

Example #1 – Single Comment Line

Let’s see some examples to have a clear sketch on the topic. It is inserted at the beginning of the code. The below example gives single line comment jut before the root element <Restaurant>, this comment provides readability for the entire document to understand the purpose of the tags.

Code:

<?XML version="1.0" encoding="utf-8"?>
<!-- In this restaurant segregating according to the facilities -->
<Restaurant>
<Nonveg>
<Resname>Dhaba Special</Resname>
<Location>ECR- II</Location>
<Serving>Buffet 100</Serving>
<Hall>Birthday</Hall>
<rating> A </rating>
</Nonveg>
<Nonveg>
<Resname>Beach Resort</Resname>
<Location>Bruce </Location>
<Serving>94 Mins -Waiter</Serving>
<Hall> Medium- Party</Hall>
<rating> B </rating>
</Nonveg>
</Restaurant>

Output:

XML Comments Example 1

Example #2 – Student Details

Code:

<?xml version="1.0" encoding="UTF-8" ?>
<!--Students  monthly assessment marks are uploaded in portal-->
<Engineering>
<ComputerDepartment>
<Sname>Ratan</Sname>
<grade>90</grade>
</ComputerDepartment>
<ECE>
<Sname>Aryan</Sname>
<grade>80</grade>
</ECE>
</Engineering>

Output:

XML Comments Example 2

2. Comments Used in Schema xsd

It can be used at any location within an element. With clear explanation, could be included between any child element of the XSD component. Comments inside Schema elements are declared in the following ways:

Comment:

<xs: element name=” book” type =”psch”>
<!—This element defines type of the book. -- >
</xs:element>

Example #1 – Comments Inside Schema Elements

Code:

<?xml version = "1.0" encoding = "UTF-8"?>
<!--Schema Generation which converts into xml file. Schema declration is w3c http://www.w3.org/2001/XMLSchema-->
<xsd:schemaxmlns:xsd = "http://www.w3.org/2001/XMLSchema"
elementFormDefault = "qualified">
<xsd:element name = "EDUCBA">
<xsd:complexType>
<xsd:sequence>
<xsd:element name = "Course" type = "xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

Output:

Corresponding xml file result is (use xsd to xml converter to see the output)

XSD to XML

Example #2 – Next Using Multiple Comment Lines in the File

Code:

<?xml version="1.0" encoding="utf-8"?>
<!-- This Xml file has comments including html or xml tags -->
<!-- Each restaurant is created using the following highlights difference
<Restaurant>
<title>This is the page where the top restaurant appears on the top</title>
<Resname>This is the name of the Restaurant who have undertaken</Resname>
<Serving>This is provided for food serving for the customers</Serving>
<Hall>The area could be alloted for party function</Hall>
<rating>This is the user review rating</rating>
</Restaurant>
-->
<Restaurant>
<Nonveg>
<Resname>Dhaba Special</Resname>
<Location>ECR- II</Location>
<Serving>Buffet 100</Serving>
<Hall>Birthday</Hall>
<rating> A </rating>
</Nonveg>
<Nonveg>
<Resname>Beach Resort</Resname>
<Location>Bruce </Location>
<Serving>94 Mins -Waiter</Serving>
<Hall> Medium- Party</Hall>
<rating> B </rating>
</Nonveg>
</Restaurant>

Output:

MultipleComment Example 2

Example #3 – Comments in between the Elements

Here the second element is not executed by the processor as it is declared as xml comments. Here during validation parser parses the first element, not the second child element.

Code:

<?xml version="1.0" encoding="ISO-8859-15"?>
<college>
<Professor>
<fullname>Daniel Cruse</fullname>
<Dept>Science</Dept>
</Professor>
<!--
<Professor>
<fullname>Naveen Raj</fullname>
<Dept>Mathematics</Dept>
</Professor>
-->
</college>

Output:

XML Comments Example 3

3. Programming Languages Employ The XML Format

Here I have used c#.XML comments in C# is used for creating complete documentation of classes. It is started with /// three slashes before any class. In the below example the tag explains as <summary> gives description and <param> implies the value for the methods and <value> defines a value.

Example #1 – Using C#

Code:

using System;
class calv
{
/// <summary>
/// check the value and update it.
/// </summary>
/// <param name="x">The given value.</param>
/// <returns>The changed value.</returns>
static int check(int x)
{
return x * 3;
}
static void Main()
{
Console.WriteLine(check(9));
}
}

Output:

C# Example 4

XML Comment Rules

Below show some rules to be covered while working with XML Comments.

  1. XML comments are not allowed to be placed before the XML Declaration as it first comes in the code. Adding reference comments should not conflict the XML declaration.
  2. The second rule is comments are not allowed to include between attribute elements. All the text should be included between less than and greater than a symbol.
  3. Nested Comments are not allowed. The entity references are not recognized within the comment line.

Conclusion

Therefore, to conclude this article is all about XML Comments and we have learned what this term is, and why XML Comments is used for. Then we proceeded with what the term meant along with the simple examples. Although it doesn’t give much clear o Comment at least we have understood some light on the topic. Finally, these comments help in providing simple context for the XML Code which is helpful for other remote developers to understand the code during communication between client and the server.

Recommended Articles

This is a guide to XML Comments. Here we discuss the Introduction to XML Comments and its types along with its examples and Code Implementation. You can also go through our other suggested articles to learn more –

  1. XML Parsers
  2. XML Versions
  3. XML Features
Popular Course in this category
XML Training (5 Courses, 6+ Projects)
  5 Online Courses |  6 Hands-n Projects |  40+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more