Skip to main content

XML error · structure

Opening and ending tag mismatch

An element was closed out of order: the parser met an end tag for an ancestor while a child was still open, so the tree cannot be built at all.

Opening and ending tag mismatch: item line 2 and channel (line 4)

What it means

XML elements must nest strictly. This 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 message carries two positions. The first — "item line 2" — is where the unclosed element was opened, and that is the useful one: the defect is a missing end tag there, not anything wrong at the line the parser gave up 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 first reported 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.

The same error elsewhere

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

  • 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 re-checked by the test suite against the real parser: the first is confirmed to produce the exact 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.