XML (eXtensible Markup Language) encodes structured data as nested elements with opening and closing tags. It's still everywhere: SOAP web services, RSS and Atom feeds, sitemaps, Office documents, Android layouts, Maven builds, SVG images and many enterprise and government data exchanges. This guide covers what makes XML well-formed, the errors you'll hit most often, and how to format and validate it.
xml<?xml version="1.0" encoding="UTF-8"?> <order id="o1" status="shipped"> <customer>Asha</customer> <items> <item sku="A-100" qty="2">Notebook</item> <item sku="B-200" qty="1">Pen & ink set</item> </items> <note><![CDATA[Leave at the <back> door]]></note> </order>
<?xml ... ?>) states the version and encoding. It's optional but recommended.<customer> hold text or other elements.sku="A-100" hold simple values on an element.& represent characters that have special meaning.<back> inside one is plain text.<br/>.<a><b></b></a> is fine; <a><b></a></b> isn't.<Item> and </item> don't match.| Character | Entity | Required? |
|---|---|---|
< | < | Always in text and attributes |
& | & | Always |
> | > | Recommended |
" | " | Inside double-quoted attributes |
' | ' | Inside single-quoted attributes |
An unescaped & is the single most common reason XML fails to parse, often in URLs (?a=1&b=2) or company names.
&.<?xml, often after concatenating files.Formatting only changes whitespace between elements. Be careful with mixed content, where text and elements are interleaved (<p>Hello <b>world</b></p>). Adding line breaks there can change how the text is displayed.
Namespaces prevent name clashes when documents combine vocabularies: <xs:element xmlns:xs="http://www.w3.org/2001/XMLSchema">. The prefix (xs) is just a shorthand. What identifies the namespace is the URI. Two documents with different prefixes bound to the same URI are equivalent.
New web APIs mostly use JSON, but XML remains the better fit for documents with mixed content, formats that need schemas, comments or namespaces, and systems that already standardize on it. See XML vs JSON: when to use which.