DEV Community

TidyContracts
TidyContracts

Posted on

XML errors: the line and column matter more than the word invalid

When an XML document fails to parse, most tools tell you one thing: invalid XML. That is the least useful part of the information - the parser already knows where it gave up. The useful output is a position, not a verdict.

These are four cases where that difference saved me real time on config dumps and API responses.

1. A mismatched closing tag deep in a dump

A 55-byte document with <orders> closed by </order> produces one error at Line 1, Column 47. Without the column you grep the whole file; with it you are looking at the character.

2. A bad character that shifts everything after it

An unescaped & breaks the rest of the document. The reported position points at the ampersand, not at the first symptom (usually several lines later).

3. Namespace prefixes that are never declared

Using <ns:item> without an xmlns:ns attribute on an ancestor is a different error from a typo, and the position tells you which element introduced it.

4. CDATA that contains the terminator

A CDATA section cannot contain the sequence that ends it. When embedded config contains it, the parser stops early and the reported line is the real cause.

What I check with

I format and validate dumps with XMLFormatter because it reports the exact line and column of each error and runs entirely in the browser, so config files with credentials never leave the machine. There is also a standalone XML validator page if you only need the error position.

When the payload inside a dump is a Base64 blob, I decode it first with this Base64 decoder before validating the result.

Top comments (0)