XML (eXtensible Markup Language)
1. Definition
XML (W3C, 1998) is a meta-markup language — a simplified subset of SGML — used to describe, store and transport structured data. Unlike HTML (fixed tags for display), XML lets you invent your own tags that describe the meaning of data.
<?xml version="1.0" encoding="UTF-8"?>
<student roll="101">
<name>Asha Patil</name>
<branch>IT</branch>
<marks semester="6">82</marks>
</student>
XML is not a presentation language: it carries data between systems (web services, config files, feeds, e-commerce), and something else (CSS/XSL, a program) presents it.
2. Key Features
- Extensible — define your own vocabulary via DTD or XML Schema.
- Self-describing — tags say what the data is; human-readable.
- Platform/vendor neutral — plain text, Unicode; any language can parse it.
- Strict, standardised syntax — one parser behaves everywhere.
- Separates data from presentation (pair with CSS/XSL for display).
- Tree (hierarchical) structure — naturally models nested records.
3. Well-Formed vs Valid XML
Well-formed (syntax rules — mandatory):
- Exactly one root element.
- Every start tag has a matching end tag (or is self-closed
). - Tags properly nested (
<b><i></i></b>, never overlapping). - Case-sensitive tag names; attributes quoted; no unescaped
< & >. - Comments
<!-- -->and PIs<?xml ... ?>written correctly.
Valid = well-formed and conforms to a DTD or XML Schema.
4. Components of an XML Document
| Component | Example | Purpose |
|---|---|---|
| Prolog | <?xml version="1.0"?> |
Version, encoding |
| Root element | <library> |
The single top node |
| Elements/children | <book>…</book> |
Data structure |
| Attributes | <book year="2020"> |
Element metadata |
| Text (PCDATA) | Asha |
Actual content |
| Entities | < & © |
Special characters |
| CDATA section | <![CDATA[ x < y ]]> |
Unparsed literal text |
| Comments/PIs | <!-- note --> <?php … ?> |
Meta information |
Namespaces avoid name clashes when mixing vocabularies:
<inv xmlns:item="http://shop.example.com/item">
<item:product>Pen</item:product>
</inv>
5. Parsing XML
| Parser | Model | Best for |
|---|---|---|
| DOM | Builds the whole tree in memory; random access | Small docs, editing |
| SAX | Event stream (start/end element callbacks); constant memory | Large docs, read-only |
| StAX / lxml / ElementTree | Pull-parsing, simpler API | Modern apps |
6. Advantages and Limitations
Advantages: simple, extensible, hierarchical, human-readable, universally supported, ideal for B2B data exchange (SOAP, RSS, config, Word/Office formats).
Limitations: verbose (large files), slower than binary JSON for JS clients, no built-in display, still needs schema validation for safety.
7. Applications
Web services (SOAP/WSDL), RSS/Atom feeds, configuration files (pom.xml, web.xml, Android layouts), data interchange in e-commerce & banking, e-books (EPUB), Office/CSV replacement, syndication, and as the base of XHTML/SVG/MathML.
8. Exam Points
- Define XML and explain its features with an example document — 7 marks.
- Explain well-formed vs valid XML with rules — 7 marks (very frequent).
- Compare HTML and XML — 5 marks.
- Explain DTD/Schema validation and DOM vs SAX parsers — 5-7 marks.