XML Validator
Validate XML syntax and check for well-formedness errors
XML Input
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.
XML Validator Use Cases
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.
| Rule | Requirement | Correct Example |
|---|---|---|
| Single root element | Document must have exactly one root | <root>...</root> |
| Matched tags | Every opening tag needs a closing tag | <p>text</p> |
| Proper nesting | Tags must close in reverse order of opening | <a><b>...</b></a> |
| Quoted attributes | Attribute values must be in single or double quotes | <el attr="val"/> |
| Entity escaping | Reserved characters must use predefined entities | < > & " ' |
| Case sensitivity | Tag names are case-sensitive: <A> is not </a> | <Book>...</Book> |
| No duplicate attributes | Each attribute name must appear once per element | <el a="1" b="2"/> |
| Valid XML declaration | If 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.
Code Examples
Validate XML programmatically in different languages. Each example checks well-formedness and returns the error message if the document is malformed.
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..." }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 Falsepackage 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>
}# 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