XML Validator
XML Validator
How the XML Validator works
Last updated: November 2026XML (Extensible Markup Language) is a text format for structured data, defined by the W3C XML 1.0 specification. An XML validator checks whether a document is well-formed — a prerequisite for any XML processing.
Paste XML into the left panel and click Validate (or just paste — it validates automatically). The right panel shows the result: element count on success, or the exact error with line/column and a fix suggestion on failure.
Well-formedness rules every XML document must satisfy:
- Every opening tag has a matching closing tag:
<item>...</item> - Tags are correctly nested — no overlapping elements
- There is exactly one root element
- Attribute values are quoted:
attr="value" - Special characters are escaped:
&for &,<for <
All validation runs in your browser using the fast-xml-parser library. No data is sent to any server.
Common XML Errors and Fixes
Tips & Tricks
Well-formed ≠ valid against a schema
An XML document can be well-formed (syntactically correct) but still fail schema validation (XSD or DTD). A well-formed document passes this validator. Schema validation additionally requires a schema file and a tool like xmllint (--schema schema.xsd) or the Python lxml library.
The XML declaration is optional but placement matters
The XML declaration <?xml version="1.0" encoding="UTF-8"?> must be the very first thing in the file — no BOM, no whitespace before it. If present, an incorrect encoding declaration can cause parsers to reject otherwise valid XML.
Five pre-defined entities you must use
XML defines five built-in entities: & (&), < (<), > (>), " ("), ' ('). The first two (& and <) are always required in text content. > is required only after ]]; the others are optional in text but required in attribute values when used as delimiters.
Self-closing tags work for empty elements
<br/> and <br></br> are equivalent in XML. Self-closing syntax is cleaner but requires a space before the slash in XHTML: <br /> (not <br/>) for maximum compatibility with older parsers.
Namespaces must be declared before use
If your XML uses namespace prefixes like <soap:Envelope>, the namespace URI must be declared with xmlns:soap="..." on that element or an ancestor. Using an undeclared prefix is a well-formedness error that many validators catch.
CDATA sections bypass entity rules
Use <![CDATA[ ... ]]> to embed content with & and < without escaping. Everything inside is treated as literal text. The only string not allowed inside CDATA is ]]> which terminates the section.
Validate XML Programmatically
Node.js (fast-xml-parser)
const { XMLValidator } = require('fast-xml-parser');
const result = XMLValidator.validate(xmlString, { allowBooleanAttributes: true });
if (result === true) {
console.log('Well-formed XML');
} else {
const { msg, line, col } = result.err;
console.error(`Line ${line}, Col ${col}: ${msg}`);
} Python (lxml)
from lxml import etree
try:
etree.fromstring(xml_bytes)
print("Well-formed XML")
except etree.XMLSyntaxError as e:
print(f"Line {e.lineno}: {e.msg}")
# Standard library (no line numbers):
import xml.etree.ElementTree as ET
try:
ET.fromstring(xml_string)
except ET.ParseError as e:
print(f"Parse error: {e}") CLI (xmllint)
# xmllint (libxml2) — available on macOS and Linux xmllint --noout file.xml # Check from stdin echo '' | xmllint --noout - # Install on macOS: brew install libxml2 # Install on Ubuntu: apt install libxml2-utils - hello
Frequently Asked Questions
- What is an XML validator?
- An XML validator checks whether an XML document is well-formed — every opening tag has a matching closing tag, attributes are properly quoted, tags are correctly nested, there is exactly one root element, and special characters are escaped as entities. It reports the exact line and column of the first error.
- What is the difference between well-formed and valid XML?
- Well-formed XML follows the basic XML syntax rules checked by a parser. Valid XML additionally conforms to a schema (DTD or XSD) that defines which elements and attributes are allowed and their structure. This validator checks for well-formedness — XSD validation requires a separate tool.
- What are the most common XML errors?
- The five most common: (1) Unclosed tags —
<item>valuemissing</item>. (2) Unescaped characters — bare&must be&; bare<in content must be<. (3) Unquoted attributes —id=123is invalid; must beid="123". (4) Multiple root elements — an XML document must have exactly one root tag wrapping all content. (5) Mismatched tag names — tag names are case-sensitive, so<Item>and</item>are not a valid pair. - Is my XML data sent to a server?
- No. All validation runs entirely in your browser using the fast-xml-parser library. Your data never leaves your machine.
- What is the difference between XML validation and JSON validation?
- XML validation checks for well-formedness (correct tag structure, attributes, nesting). JSON validation checks for correct JSON syntax (braces, quotes, commas). Both formats have strict parsers that reject any syntax error — unlike HTML parsers which are forgiving.
- How do I validate XML in Node.js?
- Use
XMLValidatorfromfast-xml-parser:const result = XMLValidator.validate(xmlString). If result is nottrue,result.errcontainsline,col, andmsg. - How do I validate XML in Python?
- Use
lxml.etree.fromstring()inside a try/except block catchingetree.XMLSyntaxError. The exception includes.linenoand.msg. The standard libraryxml.etree.ElementTreealso works but does not report line numbers. - What does 'not well-formed' mean in XML?
- An XML document is "not well-formed" when it violates basic XML syntax rules — for example, a tag is not closed, attributes are unquoted, special characters like
&or<appear in content without escaping, or there is more than one root element. Every XML parser rejects not-well-formed documents. - What is the difference between XML and HTML validation?
- XML validation enforces strict well-formedness: every tag must be closed, attribute values must be quoted, and there can only be one root element. HTML parsers (browsers) are intentionally forgiving and will render pages even with unclosed tags or missing quotes. This means invalid HTML often looks fine in a browser, while invalid XML always causes a hard parse error.
- Can I validate XML against an XSD schema?
- Yes. XSD (XML Schema Definition) goes beyond well-formedness — it validates element names, data types, attribute constraints, and structure against a contract. In Python, use
lxml.etree.XMLSchema. In Java, usejavax.xml.validation.SchemaFactory. This tool checks well-formedness only; XSD validation requires a schema file. - What is a DTD in XML?
- A Document Type Definition (DTD) is an older XML schema format that defines the allowed elements, attributes, and structure of an XML document. DTDs are declared with
<!DOCTYPE>at the top of the file. They have largely been replaced by XSD, which supports data types and namespaces. Many legacy systems (RSS, XHTML) still use DTDs. - How do I fix an "unclosed tag" error in XML?
- Every opening tag needs a matching closing tag. For example,
<item>value</item>is correct;<item>valueis not. For self-closing elements with no content, use the shorthand<item />. The error message typically includes a line number — paste your XML into this validator and the exact line will be highlighted.
Related Tools
Other free developer tools available on DevFormatter.
XML Formatter
Format, validate, and minify XML. Converts XML to JSON with one click.
JSON Lint
Detect JSON syntax errors with exact line and column positions.
YAML Validator
Check YAML syntax and detect YAML 1.1 coercion risks.
JSON Formatter
Format, validate, minify, and sort JSON. Detects duplicate keys and converts to YAML.