XML, which stands for eXtensible Markup Language, is a markup language designed to store and transport data in a structured and readable format. Unlike HTML, which is used to display data in a browser, XML is used to describe and carry data so that different systems can exchange information with each other. The most important feature of XML is that it is extensible, meaning users can define their own tags according to the needs of their data, rather than being limited to a fixed set of predefined tags.
XML was developed by the World Wide Web Consortium (W3C) as a simpler and more flexible descendant of SGML. It quickly became a standard for data exchange between applications, databases, and web services. For example, when an online shopping website needs to send order information to a bank, XML is often the format used to structure that order data. Because XML separates the data from its presentation, the same XML document can be displayed in many different ways using XSLT or processed by programs in many languages.
In this chapter, we will study the structure of an XML document, the rules for writing well-formed XML, the concept of elements and attributes, and the differences between HTML and XML. We will also learn about DTD and other advanced concepts. By the end of this chapter, students will be able to create simple XML documents that follow all the rules of well-formedness and understand why XML is so important in modern data exchange.
XML has several distinguishing features that make it ideal for data storage and transfer.
<name>Riya</name> clearly states that "Riya" is a name.These features make XML the backbone of many technologies including RSS feeds, web services using SOAP, configuration files, and data interchange between applications.
XML documents must follow certain rules to be considered well-formed. A well-formed XML document obeys all the syntax rules of XML, and only such documents can be parsed by applications.
<name> must be closed by </name>.<a><b></b></a> is correct, but <a><b></a></b> is wrong.<name> and <Name> are two different tags.<!-- comment -->.<br/> or <img/>.<?xml version="1.0" encoding="UTF-8"?>
<school>
<student roll="1">
<name>Riya Sharma</name>
<class>X</class>
<marks>92</marks>
</student>
<student roll="2">
<name>Arjun Verma</name>
<class>X</class>
<marks>88</marks>
</student>
</school>
In this document, school is the root element, student is a child element, and roll is an attribute of the student element. The first line is the XML declaration which specifies the version and encoding.
In XML, an element is the basic building block of a document. An element consists of a start tag, its content, and an end tag. Elements can contain text, other elements (called child elements), or both. The element that contains other elements is called the parent element, and the contained elements are called child elements. Elements at the same level are called siblings.
Attributes provide additional information about an element. They are written inside the start tag as name-value pairs, for example <student roll="1">. An element can have any number of attributes, but no two attributes in the same tag can have the same name, and each attribute can appear only once in a tag. The choice between using an element or an attribute depends on the nature of the data; data that is part of the content is best kept in an element, while data that describes the content is best kept in an attribute.
Although HTML and XML are both markup languages, they serve very different purposes.
<P> and <TABLE>, while XML has no predefined tags and allows users to create their own.<BR> need not be closed, but in XML every tag must be closed.These differences are frequently tested in examinations, so they should be memorised carefully.
XML is supported by a family of related technologies.
Web services such as SOAP and REST exchange data in XML format, making XML an essential technology for modern web development.
| Feature | HTML | XML |
|---|---|---|
| Purpose | Display data | Store and transport data |
| Tags | Predefined tags only | User-defined tags allowed |
| Case sensitivity | Not case-sensitive | Case-sensitive |
| Closing tags | Not always required | Always required |
| Error handling | Browsers tolerate errors | Parsers reject malformed documents |
| Whitespace | Collapsed by browsers | Preserved |
| XML Concept | Meaning |
|---|---|
| Root element | The single top-level element containing all others |
| Well-formed | Follows all XML syntax rules |
| Attribute | Extra information inside a start tag |
| DTD | Defines the valid structure of a document |
| XSLT | Transforms XML into another format such as HTML |
| XML declaration | <?xml version="1.0"?> at the start |
<br/>.<Name> and <name> are treated as different elements.<student roll=1> instead of <student roll="1">.XML is a powerful, extensible markup language designed for storing and transporting structured data. We learned that XML documents must be well-formed, meaning they must have exactly one root element, properly nested and matched tags, quoted attribute values, and case-sensitive naming. Elements and attributes provide the structure and description of the data, and technologies such as DTD, XSLT, and XPath extend the power of XML. Although HTML and XML both use tags, HTML displays data while XML carries it. This ability to exchange structured data makes XML the foundation of web services, RSS feeds, and countless business applications.