XML is strict in a way JSON is not: a single unclosed tag or an unescaped ampersand makes the whole document unreadable. This guide separates the two questions developers ask most — is the document well-formed, and is it valid — then covers namespaces, CDATA, entities and the practical value of pretty-printing.
Well-formed versus valid
Well-formed means the syntax is correct: one root element, every opening tag has a closing tag, attributes are quoted, and nesting never overlaps. Valid means the document also conforms to a declared schema — a DTD or an XSD — which constrains element names, order and datatypes.
| Check | Question it answers | Failure example |
|---|---|---|
| Well-formedness | Is this parseable XML at all? | <p>text without a closing tag |
| Validity | Does it match the agreed structure? | <price>abc</price> where xs:decimal is required |
Parsers enforce the first automatically; the second only runs when you validate against a schema. The XML Formatter & Beautifier checks well-formedness and shows exactly where parsing stops.
Namespaces and prefixes
Namespaces prevent element-name collisions by binding prefixes to URIs:
<order xmlns:inv="http://example.com/invoice"> <inv:total>42.00</inv:total></order>
Two rules catch most bugs: a prefix must be declared before use, and the URI is an identifier, not a URL you have to fetch. Default namespaces (no prefix) apply only to elements, never to attributes — an attribute without a prefix belongs to no namespace.
CDATA for literal markup
When text genuinely contains angle brackets — SQL, maths expressions, embedded scripts — wrap it in CDATA so the parser treats it as character data:
<query><![CDATA[SELECT * FROM t WHERE a < 10]]></query>
Inside CDATA you cannot nest another ]]> sequence, and entities such as & are not expanded — they stay exactly as typed. Use CDATA for opaque payloads, not as a general escape hatch.
Entities you must know
&— ampersand, required everywhere<and>— less-than and greater-than"and'— quotation mark and apostrophe — non-breaking space, useful in table-heavy documents
An unescaped & in plain text is the single most common cause of a "mismatched tag" error reported several lines away from the real mistake.
Why pretty-printing matters
Machine-generated XML often arrives as one enormous line. Indenting it makes diffs reviewable, stack traces readable, and structural errors obvious:
- Code review shows only the lines that actually changed.
- Logs and error messages point to sensible line numbers.
- Nesting mistakes stand out visually instead of hiding mid-line.
- Two documents can be compared for structural equality.
Keep the raw output for your pipeline and the formatted version for humans — the JSON to XML Converter followed by the formatter gives you both in one pass.
Frequently asked questions
Does reformatting change the document? Not semantically — whitespace between elements is insignificant unless xml:space="preserve" is set. Do not pretty-print files where exact byte output is signature-checked.
How do I validate against an XSD? Use your editor's XML validation command or a CLI tool; supply both the document and the schema file.
Why does my parser report an error on line 1? Usually a byte-order mark, an XML declaration that is not the very first thing in the file, or a missing closing tag at the very end.