Introduction to XML
XML (eXtensible Markup Language) is a markup language designed to store and transport data. Unlike HTML, which is designed to display data, XML is designed to carry data in a format that is both human-readable and machine-readable.
XML was developed by the World Wide Web Consortium (W3C) in 1998 and has since become a fundamental technology for data exchange between systems, configuration files, and document formats.
Fun Fact: XML is called "extensible" because it allows you to define your own tags, unlike HTML which has predefined tags.
XML Basic Syntax
XML documents follow strict syntax rules. Here's a simple example:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="fiction">
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<year>1925</year>
<price>10.99</price>
</book>
<book category="non-fiction">
<title>Sapiens</title>
<author>Yuval Noah Harari</author>
<year>2011</year>
<price>14.99</price>
</book>
</bookstore>XML Syntax Rules
1. Must Have Root Element
Every XML document must have exactly one root element that contains all other elements.
2. Tags Must Be Closed
Every opening tag must have a corresponding closing tag: <tag>...</tag>
3. Case Sensitive
<Book> and <book> are different tags. Be consistent with casing.
4. Proper Nesting
Tags must be properly nested: <a><b></b></a> ✓, not <a><b></a></b> ✗
5. Attribute Values Quoted
Attribute values must be in quotes: category="fiction"
6. Special Characters
Use entities: < > & " '
XML Elements and Attributes
XML data is organized using elements (tags with content) and attributes (metadata about elements).
<!-- Element with content -->
<name>John Doe</name>
<!-- Element with attribute -->
<person id="123">
<name>John Doe</name>
</person>
<!-- Self-closing element -->
<image src="photo.jpg" />
<!-- Element with multiple attributes -->
<book isbn="978-0-13-468599-1" category="programming">
<title>Clean Code</title>
</book>Common XML Use Cases
📰 RSS Feeds
News websites and blogs use RSS (Really Simple Syndication) feeds in XML format to distribute content updates to subscribers.
🔌 SOAP Web Services
Enterprise applications use SOAP (Simple Object Access Protocol) with XML for structured communication between services.
📱 Android Layouts
Android app UI layouts and resources are defined in XML files, allowing separation of design from code.
⚙️ Configuration Files
Many applications use XML for configuration: Maven (pom.xml), .NET (web.config), Spring Framework, and more.
📄 Document Formats
Microsoft Office (DOCX, XLSX), SVG graphics, and XHTML are all XML-based formats.
XML vs JSON
Both XML and JSON are used for data interchange, but they have different strengths:
| Feature | XML | JSON |
|---|---|---|
| Syntax | Verbose (tags) | Concise (brackets) |
| Attributes | Supported | Not supported |
| Comments | Supported | Not supported |
| Namespaces | Supported | Not supported |
| Best For | Documents, configs, legacy systems | APIs, web apps, modern development |
Read more: JSON vs XML: Which One Should You Use?
Parsing XML in JavaScript
You can parse XML in JavaScript using the DOMParser:
const xmlString = `
<books>
<book>
<title>Clean Code</title>
<author>Robert C. Martin</author>
</book>
</books>
`;
// Parse XML string
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "text/xml");
// Access elements
const title = xmlDoc.querySelector("title").textContent;
console.log(title); // "Clean Code"
// Get all books
const books = xmlDoc.querySelectorAll("book");
books.forEach(book => {
console.log(book.querySelector("title").textContent);
});Try Our XML Tools
Format, validate, and convert your XML data