XML Tutorial – Introduction, Structure, and Syntax Rules
In this tutorial, we’ll introduce XML data exchange format and discuss the structure of an XML document and syntax rules for XML.
What is XML?
- XML (Extensible Markup Language) is a markup language like HTML for storage or transmission of data.
- XML is widely used in web services to transport data over the network.
- XML has no predefined tags, unlike HTML.
- XML is very easy to parse and generate.
- XML provides strong support for Unicode characters. The default character encoding is UTF-8 for XML documents.
- XML defines a set of rules for encoding documents in a format which are human-friendly.
- XML is widely used in a SOA (Services Oriented Architecture).
- XML files have the extension
.xmland the media types of XML areapplication/xmlandtext/xml. - Almost all major programming languages supports XML due to its language-independent data format.
Structure of an XML document:
- An XML document contains exactly one root element: the start tag of the XML document, and it contains all other elements.
1234567891011<root><section><sub-section></sub-section><sub-section></sub-section></section><section><sub-section></sub-section><sub-section></sub-section></section><root>
- XML documents may begin with a prolog that appears before the root element. It has the metadata about the XML document, such as character encoding, document structure, and style sheets. For example,
1<?xml version="1.0" encoding="UTF-8"?>
- A tag in XML is a case-sensitive markup construct that begins with
<and ends with>. A tag can be:- A start-tag, such as
<name> - An end-tag, such as
</name> - An empty-element tag, such as
<name/>
- A start-tag, such as
- An element in XML is formed by characters between the start-tag and the end-tag. For example,
<name>John Snow</name>. It can also consist only of an empty-element tag. For example,<name/>. - XML elements can have attributes which exists within a start-tag or empty-element tag.
An attribute consists of a name–value pair. For example,1<img src="screenshot.png" alt="screenshot" />Here the names of the attributes are src and alt, and their values are screenshot.png and screenshot, respectively.
Syntax Rules:
-
Each start-tag in XML must have a matching end-tag and all tags should be properly nested, with none missing and none overlapping.
The tag names cannot contain any of the characters
!"#$%&'()*+,/;<=>?@[\]^`{|}~, nor a space character, and cannot begin with-,., or a number. - The characters
<and&holds special meaning in XML. They are key syntax characters and should not be used in an element outside aCDATAsection.
XML provides escape facilities to handle these special characters. For example:<represents<&represents&
XML has three other predefined entities:
>represents>'represents'"represents"
- An XML document cannot contain any whitespace before the XML declaration; otherwise, it will be treated as a processing instruction by the parser. XML processors preserve all white space in element content, while all whitespace within the attribute values is reported as single spaces.
- Similar to HTML, a comment in XML begins with
<!--and ends with-->.
That’s all about XML data exchange format, XML structure, and XML syntax rules.
Useful links: XML Validator, XML Formatter, XML Minifier, XML to JSON Converter
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)