XML Validator

Paste XML or upload a file to check that it is well-formed. If it is not, you get the line and column of the error, the lines around it with a pointer, and a plain-English explanation of what to change. If it is, you get a summary of the document and a neatly indented copy. Parsing happens in your browser, so the XML never leaves your machine.

Formatted XML

        
Paste XML to validate it.

How to validate XML

  1. Paste the XML into the left box, or click Upload file to open a .xml, .xsd, .svg, RSS feed, .csproj or similar file.
  2. The document is checked as you type. A green bar means it is well-formed; a red bar shows the first error with its line and column.
  3. For an error, read the explanation under the excerpt, then click Go to error to jump the cursor to that spot in the input. Fix it and the check runs again.
  4. When it passes, copy the formatted version from the right-hand box. Choose 2 spaces, 4 spaces or tabs for the indentation.

The check uses the XML parser built into your browser (DOMParser with the application/xml type), the same strict parser the browser uses for SVG and XHTML. Like every conforming XML parser, it stops at the first fatal error, so fix errors one at a time from the top.

What “well-formed” means

A well-formed XML document follows the syntax rules of the XML 1.0 specification:

Common XML errors and how to fix them

These are the messages Chrome, Edge and other Chromium browsers produce for typical mistakes (Firefox words them differently, and the explanations here cover both):

InputError messageFix
<to>Ada<body>Hi</body></note>Opening and ending tag mismatch: to line 2 and noteClose <to> before </note>.
<a/><b/>Extra content at the end of the documentWrap both in one root element.
<a id=1/>AttValue: " or ' expectedQuote the value: id="1".
<a>Tom & Jerry</a>xmlParseEntityRef: no nameWrite &amp;, or click Escape stray &.
<a>1 < 2</a>StartTag: invalid element nameWrite &lt;, or use a CDATA section.
<a>&nbsp;</a>Entity 'nbsp' not definedUse &#160;; HTML entities are not part of XML.
<ns:a/>Namespace prefix ns on a is not definedAdd xmlns:ns="…".
<a x="1" x="2"/>Attribute x redefinedRemove the duplicate.
Blank line before <?xml …?>XML declaration allowed only at the start of the documentDelete everything before the declaration, including a byte-order mark.
<a><b></b>Premature end of data in tag a line 1Add the missing </a>.
<p>hi<br></p>Opening and ending tag mismatch: br line 1 and pHTML void tags must be self-closed in XML: <br/>.

The reported line and column are where the parser noticed the problem, which can be later than the actual mistake. For a tag mismatch, the message names the line of the unclosed opening tag, so look there first. A missing quote can make the parser read several lines as one attribute value and report an error far below the real cause.

Escaping text: entities and CDATA

If you need to include code, HTML or a URL with query strings inside an element, you have two options. Escape the special characters (& becomes &amp;, < becomes &lt;), or wrap the text in a CDATA section, which the parser leaves alone:

<query><![CDATA[SELECT * FROM t WHERE a < 5 AND b & 1]]></query>

The only thing a CDATA section cannot contain is the sequence ]]>. The Escape stray & button fixes the most common case automatically: it replaces every & that does not already start an entity such as &amp; or &#160; with &amp;, and leaves CDATA sections and comments untouched.

Well-formed vs valid: XSD and DTD validation

“Well-formed” means the syntax is correct. “Valid” means the document also follows a schema, an XSD (XML Schema) or a DTD that says which elements are allowed, in what order, and what types their values must have. A document can be well-formed and still invalid, for example if a required <price> element is missing or contains text instead of a number.

This page checks well-formedness only. Browsers do not include a schema validator, and validating against an XSD in the page would mean shipping a full XML Schema engine. To validate against a schema, use one of these instead:

# libxml2 command-line tool (macOS and most Linux distributions)
xmllint --noout --schema order.xsd order.xml

# Python with lxml
from lxml import etree
schema = etree.XMLSchema(etree.parse("order.xsd"))
doc = etree.parse("order.xml")
schema.assertValid(doc)   # raises DocumentInvalid with line numbers

In Java, javax.xml.validation.SchemaFactory does the same job, and in .NET you set XmlReaderSettings.ValidationType = ValidationType.Schema. Run the well-formedness check here first; a schema validator cannot say anything useful about a document that does not parse. If you do not have a schema yet, the XML to XSD converter can draft one from a sample document.

Checking XML well-formedness in code

The same check this page runs, in your own JavaScript:

const doc = new DOMParser().parseFromString(xml, "application/xml");
const err = doc.querySelector("parsererror");
if (err) console.log("Not well-formed:", err.textContent);

Python's standard library raises an exception that carries the position:

import xml.etree.ElementTree as ET
try:
    ET.fromstring(xml_text)
except ET.ParseError as e:
    line, column = e.position   # column is 0-based here
    print(f"line {line}, column {column}: {e}")

On the command line, xmllint --noout file.xml prints nothing for a well-formed file and the error with a pointer otherwise.

Privacy

The XML is parsed by JavaScript in this page and is not uploaded. That makes it safe for configuration files, SOAP messages and exports that contain credentials or customer data. Large files of several megabytes work, though very large documents may take a moment to format.

Frequently asked questions

How do I check if my XML is valid?

Paste it into the box above or upload the file. The validator parses it immediately and shows either a green “well-formed” result or the line and column of the first error with an explanation of how to fix it.

Does this XML validator check against an XSD or DTD schema?

No. It checks well-formedness: the syntax rules every XML document must follow. Schema validation needs a schema engine such as xmllint --schema or Python’s lxml; the page explains how to use them.

Why does it only show one error?

XML parsers are required to stop at the first fatal error, because after a syntax error the rest of the document cannot be interpreted reliably. Fix the reported error and the next one, if any, appears.

Why does a single & cause an error?

In XML, & starts an entity reference such as &lt;. A bare & followed by a space or a word without a semicolon is a syntax error. Write it as &amp;, or use the Escape stray & button.

Why is the error reported on the wrong line?

The parser reports where it noticed the problem. A missing closing tag or quote is often only detected later in the file. For tag mismatches, the message includes the line of the opening tag that was never closed.

Can I validate HTML with this?

Only XHTML. Ordinary HTML allows unclosed tags like <br> and <li>, unquoted attributes and named entities such as &nbsp;, all of which are errors in XML.

Is my XML uploaded?

No. The browser’s built-in DOMParser does the parsing locally; nothing is sent to a server.