XML
XML (eXtensible Markup Language) Breakdown
XML is a structured, text-based format used for storing and transporting data. It is self-descriptive, meaning it defines its own structure with tags. Unlike JSON, XML is hierarchical and follows strict syntax rules similar to HTML but is designed for data storage, not display.
1. Basic XML Structure
An XML document consists of elements (tags), attributes, and nested elements.
Example XML Document
<?xml version="1.0" encoding="UTF-8"?>
<person>
<name>Alice</name>
<age>30</age>
<isStudent>false</isStudent>
<hobbies>
<hobby>Reading</hobby>
<hobby>Gaming</hobby>
</hobbies>
<address city="New York" zip="10001"/>
</person>
Key Features:
- Elements (
<name>,<age>) contain data. - Nested Elements (
<hobbies>contains multiple<hobby>elements). - Attributes (
city="New York"inside<address>).
2. XML Syntax Rules
XML is strict and must follow these rules:
✅ Well-formed XML
-
A root element is required
<root></root> -
Tags must be properly closed
❌ Incorrect:<name>Alice
✅ Correct:<name>Alice</name> -
Case-sensitive tags (
<Name>and<name>are different). -
Quotes for attributes
❌<address city=New York>
✅<address city="New York"> -
No special characters without escaping
-
<,>,&must be replaced with:< <!-- < --> > <!-- > --> & <!-- & --> " <!-- " --> ' <!-- ' -->
-
3. XML Elements (Tags)
XML uses tags to structure data.
Example
<user>
<id>1</id>
<name>Bob</name>
</user>
<user>is the parent element.<id>and<name>are child elements.
Empty Elements
<image src="photo.jpg"/>
- Self-closing tag (
<image/>) is valid.
4. XML Attributes vs Elements
XML allows storing data in attributes or elements.
Using Attributes (metadata style)
<person name="Alice" age="30" />
- Pros: Compact, useful for metadata.
- Cons: Harder to extend.
Using Elements (preferred for flexibility)
<person>
<name>Alice</name>
<age>30</age>
</person>
- Pros: Easier to structure and extend.
- Cons: More verbose.
✅ Best practice: Use attributes for metadata, elements for data.
5. XML Nested Elements (Hierarchy)
<company>
<employee>
<name>John Doe</name>
<role>Manager</role>
</employee>
<employee>
<name>Jane Smith</name>
<role>Developer</role>
</employee>
</company>
<company>is the root.<employee>elements are nested.
6. XML vs JSON
| Feature | XML | JSON |
|---|---|---|
| Format | <tag>value</tag> |
{ "key": "value" } |
| Readability | More verbose | More compact |
| Data Structure | Hierarchical | Key-value pairs |
| Attributes | Yes | No (but possible via nesting) |
| Namespaces | Yes | No |
| Parsing Complexity | Harder (needs XML parser) | Easier (native in JS) |
✅ JSON is better for APIs,
✅ XML is better for structured documents (e.g., RSS Feeds, Config files).
7. XML Namespaces (Avoid Conflicts)
Namespaces prevent tag conflicts when mixing different XML standards.
Example:
<root xmlns:h="http://www.w3.org/TR/html4/">
<h:table>
<h:tr>
<h:td>Data</h:td>
</h:tr>
</h:table>
</root>
xmlns:h="..."defines a namespace for HTML tags.
8. XML Schema (Validation)
To enforce a structure, we use XSD (XML Schema Definition).
Example XML Schema
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
- Ensures
personmust havename(string) andage(integer).
9. Working with XML in Different Languages
Python (xml.etree.ElementTree)
import xml.etree.ElementTree as ET
xml_data = """<person><name>Alice</name><age>30</age></person>"""
root = ET.fromstring(xml_data)
print(root.find("name").text) # Alice
JavaScript (DOMParser)
let xmlString = `<person><name>Alice</name></person>`;
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(xmlString, "text/xml");
console.log(xmlDoc.getElementsByTagName("name")[0].textContent); // Alice
PowerShell
[xml]$xml = Get-Content "data.xml"
$xml.person.name
10. Common XML Formats
- RSS Feeds (News, Blogs)
- SVG (Scalable Vector Graphics)
- SOAP (Web Services)
- KML (Google Earth)
- XHTML (Web Pages)
11. Pretty Print XML
CLI (Linux)
xmllint --format file.xml
PowerShell
[xml]$xml = Get-Content "data.xml"
$xml.OuterXml
12. When to Use XML?
✅ Best for:
- Structured documents (RSS, SVG, config files)
- Metadata (e.g., Microsoft Office
.docx,.xlsx) - Interoperability (SOAP APIs, legacy systems)
🚫 Not ideal for:
- Simple data exchange (JSON is better)
- Speed-critical applications (XML is slower to parse)
Key Takeaways
✅ XML is hierarchical (like HTML).
✅ Uses tags and attributes for data representation.
✅ Stricter than JSON (closing tags, case-sensitive).
✅ Supports schemas for validation (XSD).
✅ Used in config files, structured documents, and APIs.
Want a deeper dive into any section? 🚀