Free tool
XML Formatter and Validator
Paste XML to pretty-print it with your choice of indent, or minify it to one line. It is checked as you type: if a tag is not closed or an attribute is not quoted, you see the line, the column and the broken line. Switch to the tree to fold elements and copy the XPath of any node.
Free · no sign-up · runs in your browser, nothing is uploaded
Formatted XML appears here as you type.
What the formatter does to your XML
It parses the document, then writes it back with one element per line and each level indented. A few things are kept exactly as written, because changing them would change the data:
- Text and entities.
&,©and your own entities stay as entities. Only the whitespace used for indentation between tags is replaced. - Mixed content. When an element holds text and tags together, like
<p>Hello <b>world</b></p>, it stays on one line so no spaces are added to the text. xml:space="preserve". The XML spec uses this attribute to say whitespace inside an element matters. Those elements are left untouched.- CDATA, comments, the declaration and DOCTYPE. All kept. Turn on Remove comments to drop comments.
Empty elements are written as <a/> by default; untick the option to get <a></a>. Both mean the same thing to a parser.
Common XML errors and how to fix them
These are the rules from the XML 1.0 spec that most often break a document, with the message you see here.
| Problem | Example | Fix |
|---|---|---|
| Tag not closed or closed in the wrong order | <a><b></a> | Close inner elements first: <a><b></b></a>. |
| Attribute without quotes or value | <input checked> | Every attribute needs a quoted value: checked="checked". |
| Bare ampersand in text | Tom & Jerry | Write &amp; or wrap the text in <![CDATA[ ]]>. |
| Less-than sign in text | if a < b | Write &lt;. |
| More than one root element | <a/><b/> | Wrap everything in one parent element. |
| Mismatched case | <Item></item> | XML names are case-sensitive. Use the same case in both tags. |
| Text before the declaration | ␣<?xml version="1.0"?> | The <?xml ?> declaration must be the very first thing in the file. |
XML has five built-in entities: <, >, &, ' and ". HTML names like are not defined in XML unless a DTD declares them; use the number instead ( ).
Format and validate XML in code
Command line (libxml2). xmllint ships with macOS and most Linux distributions.
xmllint --format input.xml > pretty.xml # indent xmllint --noout input.xml # check only; prints errors with line numbers XMLLINT_INDENT=$'\t' xmllint --format input.xml # indent with tabs
Python. ElementTree.indent() was added in Python 3.9.
import xml.etree.ElementTree as ET
tree = ET.parse("input.xml") # raises ET.ParseError with line and column
ET.indent(tree, space=" ")
tree.write("pretty.xml", encoding="utf-8", xml_declaration=True)JavaScript in the browser. DOMParser does not throw on bad XML; it returns a document that contains a <parsererror> element.
const doc = new DOMParser().parseFromString(xml, "application/xml");
const err = doc.querySelector("parsererror");
if (err) console.error(err.textContent);Node.js. fast-xml-parser, the library this page uses, has a validator that reports line and column.
import { XMLValidator } from "fast-xml-parser";
const result = XMLValidator.validate(xml);
if (result !== true) console.log(result.err.line, result.err.col, result.err.msg);Using the tree view
The Tree tab shows the document as nested elements with their attributes. The first two levels are open; click the arrow to fold or unfold, or use Expand all. A collapsed element shows how many child elements it has in braces.
Hover a row and click [xpath] to copy a path that selects exactly that element, such as /orders/order[2]/customer/name. The index is added only where an element has siblings with the same name. Paste it into an XPath tester, xmllint --xpath or document.evaluate().
Need the data as JSON instead? The XML to JSON converter turns the same document into JSON with your choice of attribute and array handling. For JSON itself, use the JSON formatter.
More free developer tools
FAQ
How do I format XML?+
Paste the XML into the box above. It is indented as you type, with 2 spaces, 4 spaces or tabs. Copy the result or download it as a file. In code, use xmllint --format file.xml on the command line, or ElementTree.indent() in Python 3.9 and later.
How do I check if my XML is valid?+
Paste it above. The tool checks that it is well-formed: one root element, every tag closed and nested correctly, attribute values in quotes, and & and < escaped in text. If something is wrong you get the line and column and the broken line with a caret under the spot.
What is the difference between well-formed and valid XML?+
Well-formed XML follows the syntax rules of the XML spec, so any parser can read it. Valid XML is also well-formed and matches a schema or DTD that says which elements and attributes are allowed. This tool checks well-formedness; it does not check against an XSD or DTD.
How do I minify XML?+
Switch the mode to Minify. Whitespace between tags is removed and the document goes on one line. Text inside elements, CDATA and entities are left as they are, and you can also remove comments. The size before and after is shown under the tool.
How do I format XML in VS Code or Notepad++?+
VS Code has no built-in XML formatter; install an XML extension such as XML by Red Hat, then run Format Document (Shift+Alt+F on Windows, Shift+Option+F on macOS). In Notepad++, install the XML Tools plugin and use Plugins > XML Tools > Pretty print.
Can it format large XML files?+
Yes. The formatter makes one pass over the document, and typing is debounced more on big inputs so the page stays responsive. The preview shows the first 300 KB; copy or download gives you the full result. Nothing is uploaded.
Is my XML sent to a server?+
No. Parsing and formatting run in your browser tab. Files you open or drop are read locally. Short inputs are kept in the page URL so you can share a link; inputs over 2,000 characters are not.
Sources: W3C XML 1.0 (Fifth Edition) · Python xml.etree.ElementTree · xmllint (libxml2) · MDN: DOMParser · fast-xml-parser. Checked 2026-10-29.