XML Formatter
Developer Tools · Added
Indent an XML document so its structure is visible, or collapse it to one line for transport — and find out whether it is well-formed while you are there, with the line number of anything that is not. Elements whose content is text mixed with markup are reproduced byte for byte, because in those the spaces are part of the content rather than layout.
How to use the xml formatter
- 1Paste your XML, or press Load a sample document to see how it is treated.
- 2Choose Beautify to indent it or Minify to strip the whitespace between elements.
- 3Set the indentation, and decide whether to tidy the spacing between attributes and keep comments.
- 4Read the well-formedness panel: every unmatched or unclosed tag is listed with its line number.
- 5Copy the result or download it as a .xml file.
Examples
A nested record
- Input
- <book><title>Deep Work</title><price>499</price></book>
- Result
- <book>\n <title>Deep Work</title>\n <price>499</price>\n</book>
Elements holding only text stay on one line; only the element-holding ones are broken apart.
Mixed content is left alone
- Input
- <p>Hello <b>there</b> world</p>
- Result
- <p>Hello <b>there</b> world</p>
Indenting this would insert spaces into the sentence. The formatter detects text alongside markup and reproduces the whole element unchanged.
A mismatched tag
- Input
- <a><b></a>
- Result
- Line 1: closing tag </a> does not match <b>
The error names both tags and the line each was opened on, which is more useful than a character offset.
About the xml formatter
The whitespace problem, and how this tool decides
Every XML formatter faces the same question and most answer it badly. Whitespace between two elements almost never means anything — the newline between one record and the next is layout. Whitespace inside a run of text absolutely does. XML itself refuses to take a position, delegating it to xml:space and to the schema, neither of which a standalone formatter has.
The rule used here is to classify each element by what is inside it. If its children are all elements, the whitespace between them is layout and can be replaced with indentation. If it contains text and nothing else, the element stays on one line with its text trimmed. If it contains both text and elements, the whole subtree is reproduced exactly as it was given, character for character.
That third case is where careless formatters do real damage. Indenting the contents of a paragraph that contains an emphasis tag inserts a newline and two spaces into the middle of a sentence, and when that document is rendered the extra space shows. Detecting the case and refusing to touch it costs nothing and prevents a class of silent corruption.
Reading XML you did not write
Most XML people deal with today arrives from somewhere else — a SOAP response, an RSS feed, a configuration file, an Office document unzipped, an export from a system nobody has the source for. It is usually minified or wrapped at an arbitrary column, and the first job is always to see its shape. The element count and nesting depth reported here are a quick sanity check on that: a depth of twelve on a document you expected to be flat says something before you have read a line.
CDATA sections deserve particular care and get it. Everything between the opening and closing markers is character data by definition, including angle brackets and ampersands that would otherwise be markup, which is exactly why the section exists. A formatter that treats a CDATA body as content to be re-escaped or re-indented destroys embedded scripts, SQL and HTML alike, so the parser here consumes the whole section as one unit and never looks inside.
The other thing worth knowing when reading unfamiliar XML is that attribute order is not significant and never has been. Two documents differing only in attribute order are equivalent to a parser. If a diff is showing attribute reordering as a change, that is a formatting artefact rather than a content change, and normalising both files through the same formatter first is the way to see what really moved.
Frequently asked questions
What does well-formed actually check?
So is this a validator?
Why was my spacing inside an element left as it was?
Are external entities or DOCTYPE references fetched?
Related tools
JSON Formatter
Developer Tools
Pretty-print, minify and inspect JSON with precise error positions.
HTML Formatter
Developer Tools
Beautify and indent HTML markup, or minify it for production.
Schema Markup Generator
Developer Tools
Generate JSON-LD structured data for articles, businesses, products, events and more.