Skip to main content

XML error · InvalidTag

Expected closing tag

An element was closed out of order: the parser met a end tag for an ancestor while a child was still open.

Expected closing tag 'item' (opened in line 2, col 3) instead of closing tag 'channel'.

What it means

XML elements must nest strictly. This error means the parser found a closing tag that does not match the innermost element still open — so the document's tree cannot be built at all.

The reported position is where the unclosed element was opened, not where the parser gave up. That is the useful number: the defect is almost always a missing end tag at that point, not anything wrong at the line the message ends on.

What usually causes it

  • A missing end tag, so the next closing tag belongs to the parent instead.
  • Overlapping tags, usually from concatenating template fragments (<a><b></a></b>).
  • HTML habits in XML: <br>, <img> or <meta> written without a self-closing slash.
  • A conditional in a template that emits an opening tag on one branch only.

How to fix it

  • Start at the reported open position and close that element before its parent.
  • Self-close void elements: <br/> rather than <br>.
  • If you generate the document, build it with a serializer instead of string concatenation.
  • Ingest the broken file to see the exact open position, then fix the generator rather than the output.

The same error elsewhere

Different parsers, same defect. If you arrived with one of these messages, you are in the right place.

  • libxml2 (PHP, lxml, Nokogiri)

    Opening and ending tag mismatch: item line 2 and channel
  • Xerces (Java)

    The element type "item" must be terminated by the matching end-tag "</item>".
  • expat (Python)

    mismatched tag: line 4, column 2
  • .NET System.Xml

    The 'item' start tag on line 2 position 4 does not match the end tag of 'channel'.
  • Go encoding/xml

    element <item> closed by </channel>

Before and after

Both snippets are checked on every build: the first is confirmed to produce the error above, the second to parse cleanly.

Fails
<channel>
  <item>
    <title>First</title>
</channel>
Parses
<channel>
  <item>
    <title>First</title>
  </item>
</channel>

Where this bites most

Get started

Bring order to the XML your team can't afford to ignore.

Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.