XML Formatter
Format and beautify XML documents
XML is a markup language for structured data. Despite JSON's rise, XML remains dominant in enterprise systems, SOAP APIs, SVG, and financial messaging.
What is XML?
XML (eXtensible Markup Language) is a markup language designed to store and transport structured data. Unlike HTML, which has a fixed set of tags for displaying content, XML allows you to define your own tags — making it a meta-language for creating custom data formats.
XML was published as a W3C recommendation in 1998, designed to be both human-readable and machine-parseable. It emerged from SGML (Standard Generalized Markup Language), simplifying the complexity while retaining the power to describe structured data of any kind.
While JSON has largely replaced XML for web APIs and lightweight data exchange, XML remains deeply embedded in enterprise computing. Financial messaging (ISO 20022), healthcare (HL7 FHIR), government systems, document formats (OOXML, ODF), and vector graphics (SVG) all rely on XML as their foundation.
Elements, Attributes, and Namespaces
Elements
Elements are the building blocks of XML. An element consists of an opening tag, content, and a closing tag:
<title>Understanding XML</title>
Elements can contain text, other elements (children), or a mix of both. Empty elements can use self-closing syntax:
<separator />
Attributes
Attributes provide additional metadata about an element. They appear as name-value pairs inside the opening tag:
<book id="42" language="en">
<title>1984</title>
</book>
A common debate is whether to use elements or attributes for data. The general guideline: use attributes for metadata (IDs, types, flags) and elements for data content (names, values, descriptions).
Namespaces
Namespaces solve the problem of element name collisions when combining multiple XML vocabularies in a single document. A namespace is declared with the xmlns attribute and associated with a prefix:
<invoice xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
<cbc:ID>INV-001</cbc:ID>
<cac:AccountingSupplierParty>
<cac:Party>
<cbc:Name>Acme Corp</cbc:Name>
</cac:Party>
</cac:AccountingSupplierParty>
</invoice>
Without namespaces, an <ID> element from one schema could conflict with an <ID> element from another. Namespaces make each element globally unique by combining the namespace URI with the local element name.
Why XML Persists
Despite JSON’s dominance in web development, XML is not going away. Several characteristics keep it indispensable:
| Feature | XML Advantage |
|---|---|
| Schema validation | XSD provides rich type systems, constraints, and inheritance — far beyond JSON Schema |
| Namespaces | Enable combining multiple vocabularies in a single document without conflicts |
| Mixed content | Elements can contain both text and child elements — essential for documents and markup |
| Transformation | XSLT enables declarative transformation of XML documents into other formats |
| Mature tooling | Decades of battle-tested parsers, validators, editors, and libraries |
| Standards ecosystem | ISO 20022, SOAP, SVG, XHTML, RSS, UBL, HL7 — built on XML |
XML excels where documents need to be validated against formal schemas, transformed into different formats, or combined from multiple sources. JSON excels where simplicity, compactness, and JavaScript interoperability are priorities.
XML vs JSON
| Aspect | XML | JSON |
|---|---|---|
| Syntax | Tags with attributes | Key-value pairs |
| Verbosity | High (opening + closing tags) | Low (braces, brackets) |
| Data types | Text only (schemas add types) | 6 built-in types |
| Comments | Supported (<!-- -->) | Not supported |
| Namespaces | Full support | Not applicable |
| Schema | XSD, DTD, RelaxNG | JSON Schema |
| Parsing | DOM, SAX, StAX | JSON.parse() |
| Primary use | Enterprise, documents | APIs, configs |
| File size | Larger | Smaller |
For web APIs, JSON is the clear choice — it is more compact, natively parsed by JavaScript, and simpler to work with. For enterprise integration, document formats, and regulated industries, XML’s schema validation, namespaces, and transformation capabilities make it the better fit.
Common Use Cases
- Financial messaging: ISO 20022 messages (pacs.008, pain.001, camt.053) use XML for payment instructions, statements, and reporting across global banking networks
- SOAP web services: Enterprise APIs in banking, insurance, and government use SOAP envelopes — XML documents exchanged over HTTP
- Vector graphics: SVG (Scalable Vector Graphics) is an XML-based format used on every modern website for icons, logos, and illustrations
- Document formats: Microsoft Office (OOXML), LibreOffice (ODF), and EPUB are ZIP archives containing XML files
- Web feeds: RSS and Atom feeds use XML to syndicate blog posts, news articles, and podcast episodes
- Configuration: Maven (
pom.xml), Android manifests, Spring XML configs, and CI/CD pipelines (some Jenkins) use XML - Healthcare: HL7 FHIR and CDA documents use XML to exchange patient records and clinical data between healthcare systems
Try These Examples
A well-formed XML document demonstrating nested elements, attributes (id, genre, number), text content, and proper indentation. The XML declaration specifies version 1.0 and UTF-8 encoding.
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book id="1" genre="fiction">
<title>1984</title>
<author>George Orwell</author>
<year>1949</year>
<chapters>
<chapter number="1">The clocks were striking thirteen.</chapter>
<chapter number="2">It was a bright cold day in April.</chapter>
</chapters>
</book>
</library> A SOAP envelope using XML namespaces. The 'soap' prefix maps to the SOAP envelope schema, and the 'm' prefix maps to a custom stock service namespace. Namespaces prevent element name collisions between different vocabularies.
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header />
<soap:Body>
<m:GetPrice xmlns:m="http://www.example.org/stock">
<m:StockName>AAPL</m:StockName>
</m:GetPrice>
</soap:Body>
</soap:Envelope>