Guides  /  Developer Tools

How to Validate XML and Fix Not-Well-Formed Errors

July 6, 2026  ·  6 min read

Written by Andrian ·  Technical Writer, Static.app

Reviewed by Roman Frank ·  Technical Reviewer, Static.app

You paste XML into a parser and it throws a wall of red: "not well-formed," "junk after document element," or a line and column number that means nothing at a glance. Almost every one of these errors comes from one of seven syntax mistakes, and each has a quick, predictable fix. The free browser-based Static.app XML viewer flags input that is not well-formed and re-indents the rest, which is how you find the break.

Well-formed vs. valid: two different checks

Well-formedness is a syntax check, and validity is a schema check on top of it. A well-formed document obeys the XML syntax rules. A valid document is well-formed and also conforms to a DTD or XSD. People use the two words interchangeably, but knowing which check is failing tells you where to look.

  • Well-formed is about syntax only. The document has exactly one root element, every tag is closed, tags nest correctly, attribute values are quoted, and the special characters &, <, and > are escaped. Tag names are case-sensitive, so <Title> and </title> do not match. If any of these rules break, no XML parser will read the file at all.
  • Valid is a stricter, optional check on top of well-formedness. A valid document is well-formed and conforms to a schema, either a DTD or an XSD, that defines which elements are allowed, in what order, and with which attributes. A document can be perfectly well-formed but still invalid against a schema.
Your XML GATE ONE Well-formed? fail No parser will read the file at all pass GATE TWO OPTIONAL Valid? fail Parses cleanly, but breaks the schema pass Well-formed and valid
The order is not negotiable. A document can be perfectly well-formed and still fail its schema, but nothing can be valid without being well-formed first, because a schema validator cannot check a file the parser could not load.

Most day-to-day "my XML won't parse" problems are well-formedness problems. Fix the syntax first, and worry about schema conformance after the parser can actually load the file.

The common not-well-formed errors and how to fix each

Seven syntax mistakes account for nearly every not-well-formed error: unclosed or mismatched tags, more than one root element, unescaped ampersands and angle brackets, unquoted attribute values, stray characters before the prolog, wrong nesting order, and invalid control characters. Each one has a single mechanical repair.

WHAT BREAKS WHAT FIXES IT <item>shoes <item>shoes</item> <user/><user/> <users><user/><user/></users> Ben & Jerry Ben &amp; Jerry <book id=42> <book id="42"> Every repair adds syntax. None of them changes your data.
Four of the breaks from this section, side by side with their repairs. The pattern is worth internalizing: the fix is always mechanical, and the values you care about are never what was wrong.

Unclosed or mismatched tags

Every opening tag needs a matching closing tag with the same name and case. <item>shoes</item> is fine; <item>shoes or <item>shoes</Item> is not. Empty elements can self-close as <br/>. When a parser reports an unexpected end of file, a tag left open earlier is usually the cause.

More than one root element

An XML document must have a single top-level element that wraps everything else. Two top-level siblings with nothing around them, like <user>...</user><user>...</user>, trigger "junk after document element." Wrap them in one container such as <users>...</users>.

Unescaped ampersands and angle brackets

A raw & starts an entity reference, so Ben & Jerry inside text content is an error. Write Ben &amp; Jerry. Likewise a literal < or > in text must be written as &lt; or &gt;. If your content has a lot of markup-like text, wrap it in a <![CDATA[ ... ]]> section instead of escaping every character.

Unquoted attribute values

Every attribute value needs quotes. <book id=42> is invalid; use <book id="42">. Single or double quotes both work, as long as they match on the same attribute.

Stray characters before the prolog

The XML declaration <?xml version="1.0"?> must be the very first thing in the file, with no spaces, blank lines, or characters before it. A common hidden culprit is a UTF-8 byte-order mark (BOM) or whitespace copied in ahead of the declaration, which produces errors like "content is not allowed in prolog." Delete anything sitting before the <?xml.

THE FIRST BYTES OF THE FILE BYTE ONE Breaks BOM sp < ? x m l "content is not allowed in prolog" Parses < ? x m l the declaration is the very first thing
Both files look identical in an editor, which is what makes this error so annoying. A byte-order mark and a stray space are invisible characters that still count as content, and content is not allowed before the declaration.

Wrong nesting order

Tags must close in the reverse order they opened. <b><i>text</b></i> is out of order; the correct form is <b><i>text</i></b>. Overlapping elements are one of the most common structural mistakes when XML is hand-edited.

CORRECT <b> </b> <i>text</i> <b><i>text</i></b> Each tag closes inside the one that opened before it. OUT OF ORDER <b> </b> <i> </i> <b><i>text</b></i> The two boxes have to cross, which no tree can hold.
This is why the rule is close in the reverse order you opened. Elements are boxes inside boxes, so one can sit within another or beside it, but never half in and half out. Overlaps are the most common structural mistake when XML is edited by hand.

Invalid characters

XML 1.0 does not allow most control characters, such as a raw form feed or a null byte, even if they look invisible. These often sneak in from copy-and-paste out of another program. Removing the offending byte, or replacing it with a valid character reference, clears the error.

How the XML viewer surfaces the break

The Static.app XML viewer surfaces the break by re-indenting the document and refusing to format input that is not well-formed. Reading a raw parser error is a lot easier when you can see the document formatted. The free XML viewer is built for exactly this job, and it runs entirely in your browser: nothing you paste is uploaded to a server.

  1. Paste your XML into the input panel, or open an .xml file from your computer. There is a Sample button if you want to try it first.
  2. Click Format. The XML viewer re-indents the document with proper nesting so the structure is easy to scan, and it checks that the input is well-formed as it goes.
  3. If something is broken, the viewer flags the problem instead of quietly formatting garbage, so you can jump to the unclosed tag or stray character and fix it.
  4. Once the document is clean, copy the formatted result or download it as a file.

On the security side, the XML viewer refuses DOCTYPE declarations and external entities, which means it is safe against XXE (XML external entity) attacks. You should never "enable external DTD loading" to make a stubborn document parse; that is the exact door XXE walks through. If a file only parses with external entities turned on, treat that as a red flag in the file, not a setting to change.

After it is well-formed

Once your XML is well-formed, the Static.app XML to JSON converter turns the document into a structured JSON object. That is the shape a JavaScript app or API usually expects. And if you are weighing which format to reach for in the first place, the JSON vs. XML guide breaks down where each one fits.

Common questions

The four answers below cover what separates well-formed from valid, why a bare ampersand breaks a document, whether the XML viewer sends your data anywhere, and whether it can check a document against a schema.

What is the difference between well-formed and valid XML?

Well-formed means the document follows XML syntax rules: one root element, closed and correctly nested tags, quoted attributes, and escaped special characters. Valid means it is well-formed and also conforms to a schema (a DTD or XSD) that defines the allowed elements and structure. A document can be well-formed without being valid, but it cannot be valid without first being well-formed.

Why does my ampersand cause a not-well-formed error?

In XML, a bare & begins an entity reference, so the parser expects something like &amp; or &#38; to follow. A lone ampersand in text content breaks that expectation. Escape it as &amp;, or wrap the surrounding text in a CDATA section if it contains many special characters.

Does the XML viewer send my data anywhere?

No. The XML viewer formats and checks your XML directly in your browser, so the content is never stored on a server. It also blocks DOCTYPE and external entities, so viewing untrusted XML stays safe.

Can it check my XML against a schema?

No. The Static.app XML viewer checks well-formedness only: it confirms the syntax is correct and formats the document for easy reading. Full schema validation against a specific DTD or XSD is a separate step. Getting the document well-formed first is the prerequisite, since a schema validator cannot check a file the parser cannot even load.

Ready to try it?

Validate your XML now

Back to all guides