How to format XML
- Paste XML into the editor, drop a file on it, or click Upload.
- Click Format (or
Ctrl+Enter). Child elements are indented under their parent. - If the document isn't well-formed, the status bar shows the line, the column and what went wrong.
- Use the arrows next to the line numbers to fold elements you don't need to see.
What "well-formed" means
The validator checks that your XML follows the basic rules every XML parser requires. It does not check the document against an XSD or DTD schema.
- There is exactly one root element around everything else.
- Every opening tag has a closing tag, or closes itself like
<br/>. - Tags close in the reverse order they were opened:
<a><b></b></a>, not<a><b></a></b>. - Names are case-sensitive, so
<Item>is not closed by</item>. - Attribute values are always quoted:
id="7". - A literal
&or<in text is written as&or<.
Common error messages and what they mean
These are the messages Chrome and Edge show. Firefox words them a little differently, but the line and column work the same way.
| Message | Usual cause |
|---|---|
| Opening and ending tag mismatch | An element was closed in the wrong order, never closed, or closed with a different spelling or capitalisation. |
| AttValue: " or ' expected | An attribute value has no quotes, as in <a id=7>. |
| xmlParseEntityRef: no name | A bare & in text, such as "fish & chips". Write &. |
| StartTag: invalid element name | A < that doesn't start a tag, often from a comparison like 1 < 2 in text. |
| Extra content at the end of the document | There is more than one root element. |
| Premature end of data | The document stops before all elements are closed. Often a copy-paste that missed the last lines. |
What formatting changes and what it keeps
Each element goes on its own line, indented by two spaces, four spaces or a tab. An element that only holds text stays on one line, like <city>Boston</city>. The XML declaration, comments, CDATA sections, processing instructions and namespace prefixes such as soap:Envelope are all kept. Whitespace between elements is replaced by the new indentation, and character references like A are written out as the character they stand for.
Sorting XML
Sort XML puts child elements in alphabetical order at every level, which makes two versions of a document easy to compare side by side. Only the order of elements changes: attributes, text, comments, CDATA and namespace prefixes stay as they were. Elements that share a name, like a list of <item> entries, keep their original order relative to each other. Documents that contain a <!DOCTYPE> are rejected for security reasons, so remove it before sorting.
Converting XML to JSON
Convert to JSON turns elements into keys. Elements that repeat become an array, attributes become ordinary keys, and all values come out as strings, because XML has no number or boolean type. The root element becomes the outer object. For example:
<order id="7">
<item>pen</item>
<item>ink</item>
</order>
becomes
{
"id": "7",
"item": [
"pen",
"ink"
]
}
Frequently asked questions
Does the validator check my XML against a schema (XSD)?
No. It checks that the XML is well-formed. A document can be well-formed and still fail schema validation, for example when a required element is missing.
Will formatting change my data?
Only the whitespace between elements and the way character references are written. Text content, attribute values, comments and CDATA stay the same.
Can I format SOAP messages, RSS feeds or Android layout files?
Yes. They are all XML, and namespaced names like soap:Envelope or android:layout_width are left exactly as they are.
How do I minify XML?
Click Minify. The whitespace between tags is removed and the document is written on a single line. Text inside elements is trimmed.