Characteristics and Uses of XML
Definition
XML (eXtensible Markup Language) is a markup language designed to store and transport data in a structured and self-descriptive way.
Characteristics of XML
<name>Rahul</name>), so the meaning of data is easy to understand.Uses of XML
XML Syntax
Definition
XML syntax refers to the set of rules that must be followed while writing a well-formed XML document. These rules ensure that XML data is correctly structured and readable by both humans and machines.
Basic Rules of XML Syntax
If used, it must be the very first line in the document. The version attribute is mandatory if the declaration is present.
<?xml version="1.0" encoding="UTF-8"?>
Every XML document must have exactly one root element that contains all other elements.
Every element must have a closing tag. Unlike HTML, closing tags are mandatory.
<name>Rahul</name>
Tags must be correctly nested and must not overlap.
XML is case-sensitive. <Name> and <name> are treated as different elements.
Attribute values must be enclosed in single or double quotes.
<student id="101">
Element names should start with a letter or underscore and must not contain spaces.
Example of a Well-Formed XML Document
<?xml version="1.0" encoding="UTF-8"?>
<contact_info>
<name>Rajesh</name>
<company>TCS</company>
<phone>9333332354</phone>
</contact_info>
This example contains markup (tags) and text data inside the elements.
How to Declare XML and Rules of XML Declaration
XML Declaration
XML declaration is the optional first line of an XML document that specifies the XML version and encoding used. It provides information about the document to the parser.
Syntax<?xml version="1.0" encoding="UTF-8"?>
Attributes of XML Declaration
-
version
Specifies the XML version. Example:
version="1.0"Required - encoding Specifies character encoding such as UTF-8 or UTF-16. Optional
-
standalone
yes— document does not depend on external DTD.no— document depends on external DTD. Optional
Rules of XML Declaration
If present, it must appear at the very beginning of the XML file with no spaces or characters before it.
The keyword xml must be written in lowercase. XML is case-sensitive.
Attributes must appear in this specific order:
If the declaration is used, the version attribute must be included.
XML declaration is self-contained and does not have a closing tag.
Names like version, encoding, standalone must all be in lowercase.
Attribute values must be enclosed in single or double quotes.
Example
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<students>
<student>Rahul</student>
</students>
Difference Between XML and HTML
XML and HTML are both markup languages, but they serve different purposes. XML is mainly used to store and transport data, while HTML is used to display data on web pages.
Key Differences Between XML and HTML
| Basis | XML | HTML |
|---|---|---|
| Full Form | eXtensible Markup Language | HyperText Markup Language |
| Purpose | Stores and transports data | Displays data / web pages |
| Tags | User-defined (custom tags) | Predefined tags |
| Case Sensitivity | Case-sensitive | Not case-sensitive |
| Error Handling | Strict (errors not allowed) | Lenient (ignores minor errors) |
| Focus | Data structure and meaning | Presentation and layout |
| Closing Tags | Mandatory | Some optional |
| Validation | Supports DTD/XSD validation | No data validation |
| Extensibility | Highly extensible | Not extensible |
Example Comparison
<student>
<name>Rahul</name>
<course>XML</course>
</student>
<p>Rahul</p>
<p>XML</p>
XML describes the meaning of data, while HTML only controls its display.