Table of Contents
What is XML Formatting?
XML formatting (also called XML beautification or pretty-printing) is the process of restructuring XML data with consistent indentation, line breaks, and spacing to improve human readability. XML validators, on the other hand, check whether XML documents are well-formed and conform to specified schemas.
Raw XML from APIs or databases is often minified (no whitespace) to save bandwidth. While machines don't care about formatting, humans need properly indented XML to read, debug, and maintain it effectively.
Example: Minified vs Formatted XML
Minified XML (hard to read):
<?xml version="1.0"?><catalog><book id="1"><author>John Doe</author><title>Learning XML</title><price>29.99</price></book><book id="2"><author>Jane Smith</author><title>Advanced XML</title><price>39.99</price></book></catalog>
Formatted XML (easy to read):
<?xml version="1.0"?>
<catalog>
<book id="1">
<author>John Doe</author>
<title>Learning XML</title>
<price>29.99</price>
</book>
<book id="2">
<author>Jane Smith</author>
<title>Advanced XML</title>
<price>39.99</price>
</book>
</catalog>
Why Format and Validate XML?
1. Improved Readability
Properly formatted XML with consistent indentation makes it easy to understand the document structure at a glance. Nested elements become visually apparent.
2. Easier Debugging
When XML is formatted, syntax errors like mismatched tags or missing closing elements become immediately obvious. Debugging minified XML is painful and error-prone.
3. Better Code Reviews
Formatted XML produces cleaner diffs in version control systems, making code reviews more efficient and accurate.
4. Validation Catches Errors Early
XML validators identify well-formedness errors (syntax issues) and validation errors (schema violations) before they cause runtime failures.
5. Professional Documentation
Formatted XML in documentation, APIs, and configuration files looks professional and is easier for users to understand and work with.
Understanding XML Validation
Well-Formed vs Valid XML
There are two levels of XML correctness:
| Level | Description | Requirements |
|---|---|---|
| Well-Formed | Syntactically correct XML | Proper nesting, closed tags, quoted attributes, single root element |
| Valid | Well-formed + conforms to schema | All well-formedness rules + DTD/XSD schema constraints |
Types of XML Validation
1. Well-Formedness Check
Verifies basic XML syntax rules:
- All tags are properly opened and closed
- Elements are properly nested
- Attribute values are quoted
- Special characters are escaped (
&,<,>) - Single root element exists
2. DTD Validation
Document Type Definition (DTD) defines allowed elements, attributes, and structure:
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
3. XSD Validation
XML Schema Definition (XSD) provides more powerful validation with data types:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer"/>
</xs:complexType>
</xs:element>
</xs:schema>
5 Methods to Format XML
Method 1: Online Formatters (Fastest)
Online XML formatters provide instant beautification and validation without installation.
- Best for: Quick formatting, one-time use, learning
- Pros: No setup, instant results, visual feedback
- Cons: File size limits, internet required, privacy concerns for sensitive data
Method 2: Command-Line Tools
CLI tools enable scripting and batch processing:
- xmllint:
xmllint --format input.xml - xmlstarlet:
xmlstarlet fo input.xml - python:
python -c "import xml.dom.minidom; print(xml.dom.minidom.parse('file.xml').toprettyxml())"
Method 3: IDE Built-in Features
Most code editors include XML formatting:
- VS Code: Right-click → Format Document (or Shift+Alt+F)
- IntelliJ IDEA: Code → Reformat Code (Ctrl+Alt+L)
- Eclipse: Source → Format
- Sublime Text: XML packages via Package Control
Method 4: Programming Libraries
All major languages offer XML formatting libraries:
- Python: xml.dom.minidom, lxml
- JavaScript: DOMParser + XMLSerializer
- Java: Transformer, DOM4J
- C#: XmlDocument with XmlWriterSettings
Method 5: Build Pipeline Integration
Automate XML formatting in CI/CD:
- Prettier plugin-xml: Consistent formatting across team
- xmlformat pre-commit hooks: Auto-format before commit
- CI validation: Fail builds on invalid XML
Free Online XML Formatters
DevKits offers a free XML formatter and validator that runs entirely in your browser—secure, fast, and privacy-preserving.
🚀 Try Our Free XML Formatter & Validator
Format and validate XML instantly with our secure, client-side tool. No data leaves your browser.
Format XML Now →Code Examples
Python — Using xml.dom.minidom
import xml.dom.minidom
# Parse and pretty-print XML
dom = xml.dom.minidom.parse('input.xml')
pretty_xml = dom.toprettyxml(indent=' ')
print(pretty_xml)
# Save to file
with open('output.xml', 'w') as f:
f.write(pretty_xml)
Python — Using lxml (better for large files)
from lxml import etree
# Parse XML
tree = etree.parse('input.xml')
# Pretty print
pretty_xml = etree.tostring(
tree,
pretty_print=True,
xml_declaration=True,
encoding='UTF-8'
).decode()
print(pretty_xml)
JavaScript (Node.js) — Using DOMParser
const { DOMParser, XMLSerializer } = require('xmldom');
function formatXML(xmlString) {
const doc = new DOMParser().parseFromString(xmlString, 'text/xml');
// Pretty print manually
const format = (node, level = 0) => {
const indent = ' '.repeat(level);
if (node.nodeType === 1) { // Element
let result = indent + '<' + node.nodeName;
// Add attributes...
if (node.childNodes.length > 0) {
result += '>\n';
for (let child of node.childNodes) {
result += format(child, level + 1);
}
result += indent + '' + node.nodeName + '>\n';
} else {
result += '/>\n';
}
return result;
}
return indent + node.textContent + '\n';
};
return format(doc.documentElement);
}
Java — Using Transformer
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.Document;
import javax.xml.parsers.*;
import java.io.*;
public class XMLFormatter {
public static String formatXML(String unformattedXml) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(unformattedXml)));
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
return writer.toString();
}
}
Bash — Using xmllint
# Format XML file
xmllint --format input.xml
# Format and save
xmllint --format input.xml > output.xml
# Format with validation
xmllint --format --valid input.xml
# Check well-formedness only
xmllint --noout input.xml && echo "Valid XML"
Common XML Errors & How to Fix Them
Error 1: Mismatched Tags
The element type "item" must be terminated by the matching end-tag "</item>".
Cause: Opening tag doesn't match closing tag.
<!-- Wrong -->
<item>Content</Item>
<!-- Correct -->
<item>Content</item>
Error 2: Unclosed Tags
XML document structures must start and end within the same entity.
Cause: Missing closing tag.
<!-- Wrong -->
<root>
<element>Content
<!-- Correct -->
<root>
<element>Content</element>
</root>
Error 3: Unquoted Attributes
Attribute value must be quoted.
Cause: Attribute values must be in quotes.
<!-- Wrong -->
<element id=123>
<!-- Correct -->
<element id="123">
Error 4: Unescaped Special Characters
Entity 'amp' was not found.
Cause: Special characters must be escaped.
<!-- Wrong -->
<description>Tom & Jerry</description>
<!-- Correct -->
<description>Tom & Jerry</description>
Error 5: Multiple Root Elements
Document root element must be followed by same end tag.
Cause: XML must have exactly one root element.
<!-- Wrong -->
<root1>...</root1>
<root2>...</root2>
<!-- Correct -->
<root>
<child1>...</child1>
<child2>...</child2>
</root>
XML Best Practices
- Always Use an XML Declaration: Start files with
<?xml version="1.0" encoding="UTF-8"?> - Use Consistent Indentation: 2 or 4 spaces (not tabs) for nested elements.
- Escape Special Characters:
&→&,<→<,>→>,"→" - Use Descriptive Element Names:
<userName>instead of<un> - Validate Before Deployment: Always run validation in CI/CD pipelines.
- Use XML Schema (XSD): For complex documents, XSD provides better validation than DTD.
- Consider JSON for APIs: For new APIs, JSON is often simpler and more efficient. Use XML when schema validation or namespaces are required.
Frequently Asked Questions
What's the difference between XML formatting and validation?
Formatting makes XML readable by adding whitespace and indentation. Validation checks if XML is syntactically correct (well-formed) and/or conforms to a schema (valid).
Can formatted XML be invalid?
Yes! A formatter can make invalid XML look beautiful. Always validate XML after formatting to ensure it's actually correct.
How do I validate XML against an XSD schema?
Use tools like xmllint --schema schema.xsd file.xml or programming libraries like lxml (Python) or javax.xml.validation (Java).
Why is my XML valid but won't parse?
Possible causes: encoding mismatch, BOM (byte order mark) issues, or parser-specific limitations. Try specifying encoding explicitly and removing any BOM.
Are online XML formatters secure?
Client-side formatters (like DevKits tools) are secure—your XML never leaves your browser. Avoid uploading sensitive XML to server-based formatters.
What's the best indentation size for XML?
2 or 4 spaces are most common. Avoid tabs as they render differently across editors. Consistency matters more than the specific size.
🛠️ Ready to Format Your XML?
Use our free, secure XML formatter and validator—runs entirely in your browser.
Format XML Now →Need to convert XML? Try our JSON to XML converter and XML to JSON converter.