XML parsing guide

XML Parsing and Validation Errors Explained

Most XML parser errors mean the document is not well-formed: a tag is mismatched or unclosed, character data uses reserved markup syntax, an attribute is malformed, a namespace prefix is undeclared, or content appears where the XML document grammar does not allow it. Reduce the document to the smallest failing fragment, fix the first structural error, and parse again before investigating schema-level rules.

At a glance

Quick answer

  • Well-formed XML is a prerequisite for higher-level validation; fix parser syntax errors before reasoning about XSD, DTD, or application rules.
  • Parser locations often point to where XML became impossible to continue parsing, so inspect the preceding opening tag, attribute, entity, or delimiter too.
  • Element names and closing tags are case-sensitive, every quoted attribute needs a complete name=value pair, and one document can have only one root element.
  • Escape reserved characters in normal text and attributes, declare namespace prefixes before use, and re-run parsing after each correction so later messages reflect the repaired structure.

Separate well-formedness from schema validation

An XML parser first checks whether the document follows XML syntax: tags nest correctly, attributes are formed correctly, character references are legal, and the document has one root element. If that stage fails, the parser cannot reliably build the element tree that later validation depends on.

XSD and DTD validation answer a different question: whether an already parseable document follows a declared vocabulary or data model. VetaTool's XML validator checks well-formedness with the browser XML parser; it does not claim that an XSD, DTD business rule, or application-specific schema has been satisfied.

Well-formed but not schema-validated

<order>
  <id>42</id>
</order>

Start with tag structure and the first parser error

Opening and closing tags must match exactly, including case, and nested elements must close in reverse order. A parser may report the closing tag where it finally detects the conflict even when the original mistake is an earlier opening tag or a missing close tag.

Fix the first reported structural error before editing later lines. One missing closing tag can shift the parser's interpretation of everything that follows and create several secondary messages that disappear after the first correction.

Mismatched tags

<item>
  <name>Widget</title>
</item>

Corrected tags

<item>
  <name>Widget</name>
</item>

Check reserved characters, entities, and CDATA boundaries

A literal ampersand starts an entity or character reference, and a literal less-than sign starts markup. Normal character data therefore needs forms such as &amp; and &lt; when those characters are data instead of syntax. Entity references also need their terminating semicolon.

CDATA can hold text that would otherwise contain markup-like characters, but its closing marker must be present and the sequence ]]> cannot appear as ordinary CDATA content. Comments and processing instructions have their own closing delimiters as well.

Invalid text

<message>Tom & Jerry < 10</message>

Escaped text

<message>Tom &amp; Jerry &lt; 10</message>

Validate attributes and namespace declarations

An XML attribute needs a name, an equals sign, and a quoted value. Repeating the same attribute name on one element is not allowed, and an unescaped quote inside a value can make the parser treat the rest of the start tag as malformed markup.

A prefixed name such as api:item also needs an in-scope namespace declaration for that prefix. If a fragment was copied out of a larger document, the missing xmlns declaration may have lived on an ancestor that was not copied with it.

Declared namespace

<api:item xmlns:api="https://example.com/api" id="42" />

Confirm the document-level structure

A complete XML document has exactly one document element. Two sibling top-level elements, ordinary text before or after the root, or a DOCTYPE placed after the root element violate the document grammar even when each individual element is otherwise valid.

The XML declaration, when present, belongs at the beginning of the document. When debugging copied or concatenated payloads, inspect bytes and text around the root element as carefully as the nested content because accidental prefixes and suffixes are common causes of parse failures.

Two roots

<first />
<second />

One root

<document>
  <first />
  <second />
</document>

Use a repeatable XML repair workflow

Reduce a failing payload to the smallest fragment that still reproduces the parser error, while keeping any namespace declarations or document-level syntax the fragment depends on. Run the parser, repair only the first confirmed issue, and run it again until the document is well-formed.

  • Check tag pairing and nesting before changing data values.
  • Inspect attributes, entity references, comments, CDATA, and processing-instruction delimiters around the reported location.
  • Verify that prefixes still have namespace declarations after extracting a fragment.
  • Confirm there is one root element and no ordinary text outside it.
  • Only after parsing succeeds, move on to XSD, DTD, or application-specific validation when the receiving system requires it.