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.
<channel>
<item>
<title>First</title>
</channel><channel>
<item>
<title>First</title>
</item>
</channel>Where this bites most
Other XML errors
- Premature end of data in tag
- Extra content at the end of the document
- Unescaped ampersand (EntityRef: expecting ';')
- Entity is not defined
- Start tag expected
- XML declaration allowed only at the start of the document
- Unquoted attribute value (AttValue expected)
- Attribute redefined
- Attributes construct error (unterminated quote)
- Invalid element name
- Document is empty
- Unsupported encoding
- Sequence ']]>' not allowed in content
- Comment not terminated
- Detected an entity reference loop
- PCDATA invalid Char value (control character)
- Unescaped '<' in an attribute value
- Invalid numeric character reference
- Blank needed here
- Malformed declaration expecting version
- Unsupported XML version
- Processing instruction not terminated
- CData section not finished
- Error parsing attribute name
- Double hyphen within comment
- CharRef: invalid decimal value
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.