Turn cramped XML into cleanly indented structure, or minify it to one line. Invalid syntax? It points you at the error.
| Construct | Looks like | Purpose |
|---|---|---|
| Declaration | <?xml version="1.0"?> | Optional; states version and encoding |
| Element | <item>…</item> | The main structure; may nest freely |
| Attribute | <item id="7"> | Metadata on an element; cannot repeat, cannot nest |
| Text | <name>Alice</name> | Character data |
| CDATA | <![CDATA[ raw < text ]]> | A block where markup is treated as literal text |
| Comment | <!-- note --> | Ignored by parsers |
An element is either empty (<tag/>) or has a matching closing tag. Unlike HTML, XML is strict: every tag must close, attribute values must be quoted, and tags are case-sensitive.
Five characters are special: &, <, >, " and '. The first two are mandatory in all text; the quotes matter inside attributes. Wrapping content in CDATA avoids escaping entirely, but CDATA cannot appear inside an attribute value and carries a real cost: any embedded ]]> sequence terminates the block early.
Two vocabularies can both define a title element without conflicting, because each is bound to a namespace URI. Be aware that the prefix is only a local nickname — what actually identifies a namespace is the URI, and two different prefixes bound to the same URI are the same namespace. Comparing documents by prefix rather than by URI is a frequent and subtle bug.
Indenting XML makes it readable, but it inserts whitespace text nodes that some consumers will treat as content. For documents where whitespace is meaningful, parse the compact form and present the formatted version only for viewing.
CDATA marks a span where markup is not parsed, which is why it turns up in XML that embeds HTML, SQL or scripts. Three rules govern how it behaves:
<![CDATA[ inside the block is literal text, not a new section — there is no escaping out of it.]]>, wherever that appears. If the payload itself contains that sequence, the block has to be split around it: close the section with ]], then reopen and continue from >.For ordinary values, escaping is the safer default; CDATA earns its place only for large blocks of genuinely foreign markup where escaping every angle bracket would make the document unreadable.