XML Validator

Validate XML syntax and check for well-formedness errors

Try an example

XML Input

Runs locally · Safe to paste secrets

What is XML Validation?

An XML validator checks whether an XML document follows the structural rules defined by the XML 1.0 specification (W3C Recommendation, fifth edition). At a minimum, this xml validator confirms that a document is well-formed: it has a single root element, all tags are properly nested and closed, attribute values are quoted, and reserved characters use predefined entities. A document that fails any of these checks will cause XML parsers to throw errors rather than silently produce wrong output.

The distinction between well-formedness and validity matters. A well-formed XML document satisfies the syntax rules of the XML specification itself. A valid XML document goes further: it also conforms to constraints defined in an external schema, such as a DTD (Document Type Definition), XSD (XML Schema Definition), or Relax NG schema. This tool checks well-formedness, the first and most common validation step. Without it, nothing downstream can proceed.

XML validation catches errors before they reach production. Unclosed tags, mismatched element names, unescaped ampersands, and duplicate attributes are among the most common mistakes in hand-edited XML. Parsers in different languages handle these errors differently: some fail silently, some return partial results, and some throw exceptions. Running your XML through a validator first removes this ambiguity and gives you a clear pass-or-fail answer with error location details.

Why Use an Online XML Validator?

Catching XML syntax errors early prevents cascading failures in applications that consume XML data. A browser-based validator gives you immediate feedback without installing command-line tools or configuring IDE plugins.

Instant error detection
Paste your XML and get a pass/fail result with exact error location in under a second. No waiting for build pipelines or CLI tool setup.
🔒
Privacy-first processing
All parsing and validation happens in your browser using the DOMParser API. Your XML never leaves your machine and is never transmitted to any server.
🎯
Precise error reporting
See the exact line and column where validation fails, along with a description of the error. This is faster than reading a stack trace from a parser in your application code.
📋
No account or install required
Open the page, paste your XML, and see the result. No sign-up forms, no desktop software, no browser extensions needed.

XML Validator Use Cases

Frontend development
Validate SVG files and XHTML fragments before embedding them in React or Vue components. A single unclosed tag in an SVG can break the entire component tree.
Backend engineering
Check SOAP responses, XML-RPC payloads, and RSS/Atom feeds from third-party APIs. Validate the raw response before writing deserialization logic to avoid debugging parser exceptions at runtime.
DevOps and CI/CD
Verify that Maven pom.xml, .csproj, or Ant build files are well-formed after automated edits. A syntax error in a build config can fail an entire pipeline with a cryptic error message.
QA and testing
Validate XML test fixtures and expected-output files before running integration tests. Malformed fixtures cause false negatives that waste debugging time.
Data engineering
Check XML exports from databases, government open-data portals, and ETL pipelines. Validating structure before writing XPath queries or XSLT transforms prevents wasted effort on broken source data.
Learning XML
Students following W3C XML or XSLT tutorials can paste exercise files into the validator to check their syntax. The error messages point to the exact problem, which speeds up the learning loop.

XML Well-Formedness Rules

The XML 1.0 specification defines a strict set of syntax rules. A document must satisfy all of them to be considered well-formed. The table below lists each rule, what it requires, and a correct example. Most validation errors trace back to one of these rules.

RuleRequirementCorrect Example
Single root elementDocument must have exactly one root<root>...</root>
Matched tagsEvery opening tag needs a closing tag<p>text</p>
Proper nestingTags must close in reverse order of opening<a><b>...</b></a>
Quoted attributesAttribute values must be in single or double quotes<el attr="val"/>
Entity escapingReserved characters must use predefined entities&lt; &gt; &amp; &quot; &apos;
Case sensitivityTag names are case-sensitive: <A> is not </a><Book>...</Book>
No duplicate attributesEach attribute name must appear once per element<el a="1" b="2"/>
Valid XML declarationIf present, must be the very first line<?xml version="1.0"?>

Well-Formed vs. DTD-Valid vs. Schema-Valid

XML validation has three levels. This tool checks the first (well-formedness), which is a prerequisite for the other two.

Well-formed
Satisfies the XML 1.0 syntax rules: single root, matched tags, quoted attributes, proper nesting. Every XML parser checks this first. If a document isn't well-formed, it isn't XML at all.
DTD-valid
Conforms to a Document Type Definition that specifies which elements and attributes are allowed, their order, and their cardinality. DTDs are declared inline or via a DOCTYPE reference. DTD validation is common in legacy systems and XHTML.
Schema-valid (XSD / Relax NG)
Conforms to an XML Schema Definition (XSD) or Relax NG schema. These schemas support data types (integer, date, URI), namespaces, and complex content models. XSD validation is standard in SOAP web services, HL7 healthcare data, and enterprise integration.

Code Examples

Validate XML programmatically in different languages. Each example checks well-formedness and returns the error message if the document is malformed.

JavaScript (DOMParser)
function validateXML(xmlString) {
  const parser = new DOMParser()
  const doc = parser.parseFromString(xmlString, 'application/xml')
  const error = doc.querySelector('parsererror')
  if (error) {
    return { valid: false, message: error.textContent }
  }
  return { valid: true, message: 'Well-formed XML' }
}

validateXML('<root><item>hello</item></root>')
// → { valid: true, message: "Well-formed XML" }

validateXML('<root><item>hello</root>')
// → { valid: false, message: "Opening and ending tag mismatch..." }
Python
import xml.etree.ElementTree as ET

def validate_xml(xml_string):
    try:
        ET.fromstring(xml_string)
        return True, "Well-formed XML"
    except ET.ParseError as e:
        return False, str(e)

valid, msg = validate_xml('<root><item>hello</item></root>')
# → (True, "Well-formed XML")

valid, msg = validate_xml('<root><item>hello</root>')
# → (False, "mismatched tag: line 1, column 27")

# With lxml — also supports XSD schema validation
from lxml import etree
schema = etree.XMLSchema(etree.parse('schema.xsd'))
doc = etree.fromstring(b'<root><item>hello</item></root>')
schema.validate(doc)  # → True or False
Go
package main

import (
    "encoding/xml"
    "fmt"
    "strings"
)

func validateXML(raw string) error {
    decoder := xml.NewDecoder(strings.NewReader(raw))
    for {
        _, err := decoder.Token()
        if err != nil {
            if err.Error() == "EOF" {
                return nil
            }
            return err
        }
    }
}

func main() {
    err := validateXML("<root><item>hello</item></root>")
    fmt.Println(err) // → <nil>

    err = validateXML("<root><item>hello</root>")
    fmt.Println(err) // → XML syntax error: unexpected end element </root>
}
CLI (xmllint)
# Check well-formedness (part of libxml2, pre-installed on macOS/Linux)
xmllint --noout document.xml
# Exit code 0 = valid, non-zero = errors printed to stderr

# Validate against an XSD schema
xmllint --noout --schema schema.xsd document.xml

# Validate against a DTD
xmllint --noout --dtdvalid schema.dtd document.xml

# Validate from stdin
echo '<root><unclosed>' | xmllint --noout -
# → :1: parser error : Premature end of data in tag unclosed line 1

Frequently Asked Questions

What is the difference between well-formed and valid XML?
Well-formed XML follows the syntax rules of the XML 1.0 specification: one root element, matched and properly nested tags, quoted attributes. Valid XML goes further by also conforming to a DTD or XSD schema that defines which elements, attributes, and data types are allowed. A document can be well-formed but not valid if it has correct syntax but breaks a schema constraint.
Can an XML document be valid but not well-formed?
No. Well-formedness is a prerequisite for validity. If a document breaks any XML syntax rule (unclosed tag, unquoted attribute, etc.), parsers cannot build a tree from it, so schema validation cannot even begin. Fix well-formedness errors first, then check against a schema.
What are the most common XML validation errors?
The five most frequent errors are: unclosed or mismatched tags, unescaped ampersands in text content, unquoted attribute values, missing root element, and incorrect case in tag names (XML is case-sensitive). Most of these are caused by hand-editing XML or by string concatenation in code instead of using a proper XML serializer.
Is XML validation the same as XML linting?
They overlap but are not identical. Validation checks whether a document conforms to the XML specification (well-formedness) or a schema (DTD/XSD). Linting typically goes further by also flagging style issues: inconsistent indentation, unused namespace declarations, or redundant default attribute values. xmllint from libxml2 combines both: --noout checks well-formedness, while --schema adds XSD validation.
How does XML validation differ from JSON validation?
XML has a stricter and more complex grammar than JSON. XML requires matched opening/closing tags, supports attributes, namespaces, mixed content (text and elements together), and has multiple schema languages (DTD, XSD, Relax NG, Schematron). JSON validation checks brace/bracket matching, quoted keys, and comma placement. JSON Schema exists but is simpler than XSD. An XML document is typically 2-3 times larger than the equivalent JSON for the same data.
Does this tool validate against XSD or DTD schemas?
This tool checks well-formedness only: it confirms that your XML follows the syntax rules of the XML 1.0 specification. It does not validate against external DTD or XSD schemas. For schema validation, use xmllint --schema on the command line, or lxml in Python, which supports both XSD and Relax NG.
Why does my XML fail validation when it looks correct?
The most common invisible causes are: a byte order mark (BOM) before the XML declaration, a stray character after the closing root tag, an unescaped ampersand inside text content (use &amp; instead), or a namespace prefix that was used but never declared. Copy your XML into a hex viewer or run it through xmllint --noout to get the exact byte offset of the error.