Format, minify and check XML
This XML formatter, or beautifier, takes XML that is hard to read, such as a whole file on one line from an API or an export, and lays it out with one element per line and clear indentation. Minify does the opposite and removes the white space between elements, which makes the file smaller for sending or storing. On the way, your browser's XML parser checks that the document is well-formed, so the tool works as a quick XML validator and checker too. It checks well-formedness only; to validate a document against a DTD or schema you need another tool.
To turn XML into JSON or JSON into XML, use the XML to JSON converter. For JSON files, the JSON formatter does the same job as this page.
Options explained
- Pretty print or Minify. Pretty print puts each child element on its own line, indented by 2 spaces, 4 spaces or a tab per level. Minify writes the document without line breaks or indentation between elements.
- Keep comments. Comments stay where they are. Switch it off to remove them all. Text on both sides of a removed comment is joined again.
- Collapse empty elements. Writes an element with no content as <tag/>. Switched off, it is written as <tag></tag>. The XML 1.0 spec allows both forms for an empty element.
- Sort attributes by name. Puts namespace declarations (xmlns) first and the other attributes in alphabetical order. The same section of the spec says the order of attributes in a tag is not significant, so sorting does not change the meaning. It helps when you compare two versions of a file.
- Keep the XML declaration. The <?xml version="1.0"?> line is copied as it was written. Switch it off to leave it out.
- Show tree view. Shows the elements as a tree you can open and close, with their attributes and short text values, a quick viewer for the structure of a file.
What happens to white space
Formatting means changing the white space between elements, so the tool only touches white space where no text is lost:
- Between child elements that hold no text of their own, white space is replaced by line breaks and indentation, or removed when you minify.
- Elements with only text keep their text exactly, including spaces at the start and end.
- Elements that mix text and child elements, like a paragraph with a bold word, stay on one line, with every space kept. Adding line breaks there would add spaces to the text.
- Inside an element with xml:space="preserve", everything is copied as it is. The spec describes this attribute as signaling that applications should preserve all white space. xml:space="default" inside it turns formatting back on.
The output is built from what the parser read, so a few things come out spelled differently but mean the same: character references like © are written as the character, attribute values are always in double quotes, and the characters &, < and > in text are escaped. CDATA sections, processing instructions and the DOCTYPE are kept as they were.
Errors by line and column
The tool checks well-formedness: the syntax rules every XML document must follow. It does not check the document against a DTD or an XML schema. The browser's parser does the check, and MDN describes that for XML that is not well-formed it returns a document with a parsererror node describing the error. The tool reads the message, the line and the column from that node where the browser gives them. The most common causes are:
| Problem | Fix |
|---|---|
| A plain & in text, as in Salt & Pepper | Write & |
| An HTML entity such as | Use   or the character itself |
| An end tag that does not match, or a missing end tag | Close elements in the reverse order you opened them |
| Two elements at the top level | Wrap them in one root element |
| An attribute value without quotes | Put the value in double or single quotes |
| Space or a line before <?xml ...?> | Make the declaration the very first thing in the file |
Nothing in the XML is run
The tool only reads the XML as text. It never inserts the parsed document into the page, applies no XSLT stylesheet (an xml-stylesheet instruction stays in the output as text) and does not request any file named in a DOCTYPE. The XML 1.0 spec requires only validating processors to read the external DTD and external entities. MDN notes that parseFromString builds a separate in-memory document in which script elements are disabled.
A worked example
An online shop exports its product feed as one long line of 40 KB. Paste it, or open the file, and the result reads Well-formed XML with the number of elements and attributes. The output shows each product on its own lines, indented by 2 spaces. Turn on Sort attributes, download the file as products-formatted.xml and do the same with last week's export: the two files now line up for a side-by-side comparison. Before sending the feed on, switch to Minify, turn off Keep comments and copy the result.
Limits
Files up to 20 MB can be opened. Everything runs on the main page, so a file of several megabytes can take a moment, and phones and tablets have less memory for very large files. The preview shows the first 200,000 characters; Copy and Download include everything. The tree view shows up to 1,500 elements. Elements nested more than 2,000 levels deep are not formatted. The file is read as UTF-8 text.
How to use the XML formatter
- 1Paste your XML, drop a file on the box or open one.
- 2If an error appears, read the explanation, jump to the line and column, and fix it. The result updates as you type.
- 3Choose Pretty print with an indent, or Minify, and set the options for comments, empty elements, attributes and the declaration.
- 4Copy the XML or download it as a .xml file.
Sources
- W3C: Extensible Markup Language (XML) 1.0, Fifth Edition (empty-element tags and attribute order in section 3.1, xml:space in section 2.10, processor types in section 5.1)
- MDN: DOMParser.parseFromString(), the parsererror node and the inert document
Related guides
- JSON vs YAML vs CSV: Which Format for Which Job
- JSON Syntax Errors: What Breaks a Parser and How to Fix It
- Robots.txt vs Noindex vs Canonical: Which One to Use
Frequently asked questions
Is my XML uploaded anywhere?
No. Your browser parses and formats the XML on your device, and nothing is sent to a server. Copy link only shares your settings, never the text.
Does formatting change my data?
Only the white space between elements changes, which is what formatting means. Text inside elements is kept as it is, elements that mix text and child elements stay on one line, and everything inside xml:space="preserve" is copied exactly. Character references like © are written as the character itself, and the five characters XML needs escaped are escaped again.
What is the difference between well-formed and valid XML?
Well-formed means the document follows the XML syntax rules: one root element, every element closed in the right order, quoted attribute values and no stray & or <. Valid means it also follows a DTD or schema. This tool checks that the XML is well-formed; it does not check it against a DTD or schema.
Why does my error message look different in another browser?
The error text comes from the browser's own XML parser, and each browser words it differently. The tool reads the line and column from that message where the browser gives them, adds a plain explanation for common mistakes and offers a Jump to error button.
Is it safe to paste XML from an unknown source?
The tool does not run or load anything named in the XML. It never inserts the parsed document into the page, applies no stylesheets (an xml-stylesheet instruction is kept as text) and does not request files named in a DOCTYPE. The output is plain text.
Can I sort attributes or remove comments?
Yes. Sort attributes puts namespace declarations first and the other attributes in alphabetical order, which makes two files easier to compare. The XML spec says the order of attributes is not significant, so this does not change the meaning. Keep comments off removes every comment.
How large can the file be?
Up to 20 MB. Larger files take longer, because everything runs on your device. Phones and tablets have less memory, so very large files can be slow there. The tree view shows only the first 1,500 elements.