XML Formatter
Pretty-print XML with proper indentation, validate it, or minify to one line.
Working with XML
XML predates JSON as the standard data interchange format and remains deeply embedded in enterprise systems, document formats and configuration. SOAP APIs, RSS feeds, SVG images, Android layouts, Maven builds and Office documents are all XML underneath.
This tool pretty-prints XML with consistent indentation, minifies it, and reports whether the document is well-formed.
Key takeaways
- Well-formed means syntactically correct; valid means it also matches a schema.
- XML is case-sensitive and every element must be closed.
- Five characters must be escaped: & < > " and '.
- Attributes cannot repeat within an element and must be quoted.
- Whitespace between elements may or may not be significant, depending on the schema.
Well-formed versus valid
| Well-formed | Valid | |
|---|---|---|
| Checks | XML syntax rules | Conformance to a DTD or XSD schema |
| Elements properly nested and closed | Required | Required |
| Correct element names in the right order | Not checked | Required |
| Correct data types in attributes | Not checked | Required |
| Needs a schema | No | Yes |
This tool checks well-formedness. Schema validation requires the corresponding XSD or DTD and is done with a dedicated validator.
XML syntax rules
- Exactly one root element containing everything else.
- Every element must be closed - either
<p>...</p>or the self-closing<br/>. - Elements must nest properly. Overlapping tags are invalid, unlike in forgiving HTML.
- Names are case-sensitive.
<Name>and<name>are different elements. - Attribute values must be quoted, with single or double quotes.
- Attribute names cannot repeat within the same element.
- Names must start with a letter or underscore, never a digit, and cannot begin with "xml" in any casing.
The five escaped characters
| Character | Entity | Must escape? |
|---|---|---|
| & | & | Always |
| < | < | Always |
| > | > | Recommended |
| " | " | Inside double-quoted attributes |
| ' | ' | Inside single-quoted attributes |
An unescaped ampersand is the single most common cause of XML parse failures, because ampersands appear constantly in URLs inside attributes.
<![CDATA[ ... ]]>. They are useful for embedding code or markup. The one thing they cannot contain is the sequence ]]> itself.XML compared with JSON
| XML | JSON | |
|---|---|---|
| Verbosity | High - closing tags repeat every name | Low |
| Attributes | Yes, distinct from child elements | No - everything is a key |
| Comments | Yes | No |
| Namespaces | Yes | No |
| Schema validation | Mature - XSD, DTD, RelaxNG | JSON Schema, newer |
| Native to browsers | Requires parsing | Parses directly to objects |
| Mixed content | Handles text mixed with markup | Awkward |
| Typical use today | Documents, enterprise systems, config | Web APIs |
XML remains the better choice for document-shaped data with mixed text and markup. JSON won the API space because it maps directly onto programming language data structures.
Where you still meet XML
- SVG - vector graphics are XML documents you can edit in a text editor.
- RSS and Atom feeds.
- Office documents. A .docx or .xlsx file is a zip archive full of XML.
- Android layouts and manifests.
- Maven, Ant and many build tools.
- SOAP web services, still widespread in banking, insurance and government.
- XML sitemaps for search engines.
Security notes
A related risk is the "billion laughs" attack, in which nested entity definitions expand exponentially and exhaust memory. Disabling DTD processing addresses both.
Frequently Asked Questions
What does formatting XML do?
It adds consistent indentation and line breaks so the document structure is readable, without changing any content. Minifying does the reverse, stripping whitespace between elements to reduce size.
What is the difference between well-formed and valid XML?
Well-formed means the document follows XML syntax rules - one root element, everything closed and properly nested, attributes quoted. Valid additionally means it conforms to a schema defining which elements may appear where. This tool checks well-formedness.
Why does my XML fail to parse?
The most common cause is an unescaped ampersand, which appears constantly in URLs inside attributes and must be written as &. Other frequent causes are unclosed elements, mismatched case in tag names, and more than one root element.
Which characters must be escaped in XML?
Ampersand and less-than always, greater-than by convention, and quote characters inside attribute values using the matching quote style. There are five predefined entities: &, <, >, " and '.
What is a CDATA section?
A block that tells the parser to treat its contents as literal text with no markup interpretation, written as an opening CDATA marker and a closing one. It is useful for embedding code or HTML without escaping every character. It cannot contain the closing sequence itself.
Is XML still used?
Extensively. SVG graphics, RSS feeds, Office documents, Android layouts, Maven builds, XML sitemaps and SOAP web services are all XML. JSON displaced it for web APIs, but XML remains dominant for documents and in enterprise systems.
Should I use XML or JSON?
JSON for web APIs, where it maps directly onto programming language data structures and is far less verbose. XML for document-shaped data with mixed text and markup, where you need namespaces, comments or mature schema validation.
What is an XXE vulnerability?
XML External Entity processing allows a document to instruct the parser to read local files or make network requests. Many parsers historically enabled it by default, and it has been used to read server configuration and reach internal networks. Disable external entities and DTD processing when parsing untrusted XML.