How to Use the XML Formatter & Validator
Using our free XML Formatter is straightforward. Simply paste your raw, poorly formatted, or minified XML into the Input XML panel on the left, then click the Format & Validate button to instantly receive a properly indented, human-readable version in the Formatted Output panel.
Step-by-Step Guide
- Paste your XML — Copy any XML string and paste it into the left editor panel.
- Click "Format & Validate" — The tool will simultaneously format the XML with proper indentation and validate its structure.
- Review the result — If your XML is valid, you'll see the formatted output on the right and a ✓ Valid XML badge. If there are errors, they'll be listed clearly below the toolbar.
- Copy the output — Use the ⎘ Copy button in the output panel header to copy the formatted XML to your clipboard.
- Use "Load Sample" — If you want to try the tool without your own data, click 📄 Load Sample to insert a working XML example.
What is XML Validation?
XML (eXtensible Markup Language) validation ensures a document conforms to specific structural rules. There are two levels of XML correctness:
1. Well-Formed XML
A well-formed XML document follows the basic syntactic rules of the XML specification:
- Single root element — The entire document must be wrapped in one root element (e.g.,
<root>...</root>). - Properly closed tags — Every opening tag must have a matching closing tag.
<item>value</item>is correct;<item>valueis not. - Case sensitivity — XML tag names are case-sensitive.
<Title>and<title>are treated as different elements. - Proper nesting — Tags must not overlap.
<a><b></a></b>is invalid. Correct:<a><b></b></a>. - Quoted attributes — Attribute values must always be in quotes:
<book id="1">not<book id=1>. - Special characters escaped — Characters like
<,>,&,",'in text content must be escaped as<, >, &, ", '.
Code Snippets
Parse and Validate XML in JavaScript (Browser)
// Using the native DOMParser API — no libraries needed
function validateXml(xmlString) {
const parser = new DOMParser();
const doc = parser.parseFromString(xmlString, 'application/xml');
const parserError = doc.querySelector('parsererror');
if (parserError) {
return { valid: false, error: parserError.textContent };
}
return { valid: true, error: null };
}
Format XML with Proper Indentation in Node.js
function formatXml(xml) {
let formatted = '';
let indent = 0;
const tab = ' ';
xml.replace(/>\s*</g, '><')
.split(/(<[^>]+>)/)
.filter(Boolean)
.forEach(node => {
if (node.match(/^<\//)) {
indent--;
formatted += tab.repeat(Math.max(indent, 0)) + node + '\n';
} else if (node.match(/^<[^?!].*[^/]>$/)) {
formatted += tab.repeat(indent) + node + '\n';
indent++;
} else {
formatted += tab.repeat(indent) + node.trim() + '\n';
}
});
return formatted;
}
Deep Dive: XML Schemas vs. Well-Formedness
When validating XML, developers often confuse well-formedness with schema validation. Well-formedness simply means that the XML parser can read the document hierarchy without crashing. Schema validation, on the other hand, checks whether the elements, structures, and data types conform to a strict template defined by a Document Type Definition (DTD) or an XML Schema Definition (XSD).
For example, a schema might enforce that an <age> element must contain only positive integers, or that a <product> element must contain exactly one <price> element. While our browser-based tool validates well-formedness, complex enterprise systems usually perform both checks to guarantee data integrity across legacy integrations.