Skip to main content
About

Introduction to XML: Syntax, Use Cases, and When to Use It

XML (eXtensible Markup Language) has been a cornerstone of data exchange on the internet for decades. While JSON has captured the spotlight in modern web development, XML remains essential in enterprise systems, SOAP web services, document formats, and data standards across industries. Understanding XML is critical if you work with enterprise systems, legacy code, SOAP APIs, or document-based data.

This comprehensive guide introduces XML from the ground up: syntax, structure, real-world applications, comparison with JSON, and when to choose XML for your projects.

What is XML?

XML (eXtensible Markup Language) is a markup language that uses custom tags to describe data. Unlike HTML (which uses predefined tags like <p>, <div>, <span>), XML lets you define your own tags to describe your data structure.

<?xml version="1.0" encoding="UTF-8"?> <book> <title>XML Guide</title> <author>Z Marin</author> <year>2025</year> <pages>350</pages> </book>

The key insight: XML is self-describing. You can understand the data structure just by reading the tags.

XML Syntax Fundamentals

XML Declaration

Every XML document should start with a declaration:

<?xml version="1.0" encoding="UTF-8"?>

This tells the parser which XML version to use and the character encoding (usually UTF-8).

Tags and Elements

Tags are enclosed in angle brackets. Every opening tag must have a closing tag:

<name>John Doe</name> <age>30</age> <email>john@example.com</email>

Attributes

Tags can have attributes that provide additional information:

<person id="123" type="employee"> <name>John Doe</name> </person>

Nesting and Hierarchy

XML supports unlimited nesting to represent hierarchical data:

<company> <department name="Engineering"> <employee id="001"> <name>Alice</name> <salary>100000</salary> </employee> <employee id="002"> <name>Bob</name> <salary>95000</salary> </employee> </department> </company>

Special Characters and Escaping

XML reserves certain characters (<, >, &, ", '). Escape them using entities:

&lt; represents < &gt; represents > &amp; represents & &quot; represents " &apos; represents '

Example: <description>5 &lt; 10 &amp; 10 &gt; 5</description>

Comments

XML supports comments:

<!-- This is a comment --> <data>value</data>

Real-World XML Use Cases

1. SOAP Web Services

SOAP (Simple Object Access Protocol) uses XML for messaging:

<?xml version="1.0"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap-envelope/"> <soap:Body> <GetUserResponse xmlns="http://example.com"> <userId>123</userId> <name>John Doe</name> </GetUserResponse> </soap:Body> </soap:Envelope>

Many enterprise systems still use SOAP/XML for service-to-service communication, especially in banking, healthcare, and government.

2. SVG (Scalable Vector Graphics)

SVG is XML-based format for vector graphics:

<svg width="100" height="100"> <circle cx="50" cy="50" r="40" fill="blue" /> <rect x="10" y="10" width="30" height="30" fill="red" /> </svg>

3. RSS and Atom Feeds

Blog and news feeds use XML:

<?xml version="1.0"?> <rss version="2.0"> <channel> <title>My Blog</title> <item> <title>First Post</title> <description>Article content...</description> <pubDate>Thu, 24 Oct 2025 10:00:00 GMT</pubDate> </item> </channel> </rss>

4. Office Documents

Modern Office formats (DOCX, XLSX, PPTX) are actually XML files zipped together. Word documents and Excel spreadsheets are XML-based internally.

5. Configuration Files

Many systems use XML for configuration (Maven, Spring Framework, Android manifests):

<?xml version="1.0"?> <configuration> <database> <host>localhost</host> <port>5432</port> <name>myapp_db</name> </database> </configuration>

6. Data Exchange and Standards

XML is used in countless industry standards: OpenAPI/Swagger (YAML/XML), UBL (e-invoicing), EDI (Electronic Data Interchange), HL7 (healthcare data), and more.

XML vs. JSON: Key Differences

Aspect XML JSON
Verbosity Very verbose (tags repeat) More compact
Parsing DOM/SAX parsing available Built-in to most languages
Schema Validation XSD, RELAXNG, Schematron JSON Schema
Attributes Supports attributes No attributes (use keys)
Comments Supported Not standard (JSON5 adds them)
Modern APIs Legacy systems Web APIs standard

XML Validation

XSD (XML Schema Definition)

XSD defines valid XML structure, data types, and constraints:

<?xml version="1.0"?> <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>

DTD (Document Type Definition)

Older validation method (mostly replaced by XSD), but still used in some systems:

<!DOCTYPE book [ <!ELEMENT book (title, author, year)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT year (#PCDATA)> ]>

When to Use XML

Use XML when:

  • Integrating with enterprise systems (banks, government, healthcare)
  • Working with SOAP web services
  • Using standards that require XML (HL7, UBL, XBRL, etc.)
  • Data needs comprehensive schema validation (XSD)
  • Building SVG graphics or document-based formats
  • Working with legacy systems that expect XML
  • You need to represent both data and metadata in the same document

Don't use XML when:

  • Building modern REST APIs (use JSON)
  • File size is critical (XML is verbose)
  • You're starting a new project without XML requirements
  • Your team is unfamiliar with XML

Converting Between XML and JSON

You can convert between XML and JSON, though neither is perfectly lossless:

// XML: <person> <name>John</name> <age>30</age> </person> // Equivalent JSON: { "person": { "name": "John", "age": 30 } }

Use our XML to JSON converter for instant conversion.

Parsing XML in Your Code

Python Example

import xml.etree.ElementTree as ET xml_data = '''<?xml version="1.0"?> <book> <title>XML Guide</title> <author>Z Marin</author> </book>''' root = ET.fromstring(xml_data) print(root.find('title').text) # Output: XML Guide

JavaScript Example

const parser = new DOMParser(); const xmlDoc = parser.parseFromString(xmlString, "text/xml"); const title = xmlDoc.getElementsByTagName("title")[0].textContent;

Key Takeaways

  • XML is self-describing: Tags make data structure clear
  • Verbose but powerful: More overhead than JSON but more validation options
  • Enterprise standard: Dominant in legacy systems and certain industries
  • Schema validation: XSD provides strong validation capabilities
  • Still relevant: SOAP, SVG, OOXML documents, industry standards
  • Not for new APIs: Use JSON for modern REST APIs
  • Choose wisely: Context and requirements should drive format choice

Next Steps

If you need to work with XML:

  1. Understand the basics covered here (syntax, structure, tags)
  2. Learn XSD validation if working with strict schemas
  3. Use XML parsers appropriate to your language
  4. Consider converting to JSON if building new systems
  5. Use our XML tools for quick validation and conversion

XML's role has evolved—it's no longer the go-to format for everything, but it remains essential in enterprise systems and specific domains. Understanding XML makes you versatile in integrating legacy and modern systems.