Nifty Dev Tools
  1. Home
  2. XML Formatter
Format

Paste messy XML and get it tidy and checked, with the exact spot of any error, right in your browser.

Input XML
Output

Your neatly indented XML will show up here.

How to use the XML Formatter

  1. Paste your XML into the input, press Open file, or drag an .xml file (or .svg, .rss, .xsd and friends) onto the input box. Press Example to just try it out. We check it as you type.
  2. If it isn't well-formed, the message below the input says what is wrong and where: the line, the column and a plain reason, like which tag was left open and on which line. Press Show me to jump straight to it; the spot is underlined in red. If it's a common mistake, press Fix it (or Fix all) and it's fixed for you: the note above the input lists every change. Keep closes it, Undo brings your original back.
  3. Press Format (or Ctrl + Enter, ⌘ + Enter on a Mac) to indent it with 2 spaces, 4 spaces or tabs, or Minify to put it on one line without the layout.
  4. Copy or download the result.

Example

One long line, the way an API or a build tool often writes it:

<?xml version="1.0" encoding="UTF-8"?><catalog><!-- spring list --><book id="bk101" lang="en"><title>XML &amp; You</title><price currency="EUR">39.95</price><blurb>A <em>friendly</em> guide.</blurb></book><book id="bk102"/></catalog>

Formatted with 2 spaces:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <!-- spring list -->
  <book id="bk101" lang="en">
    <title>XML &amp; You</title>
    <price currency="EUR">39.95</price>
    <blurb>A <em>friendly</em> guide.</blurb>
  </book>
  <book id="bk102"/>
</catalog>

The <blurb> stays on one line: it mixes text with an <em> tag, so its inside is kept exactly as you wrote it (see below). Minify turns the formatted version back into the first one, character for character.

What formatting changes, and what it never touches

Only the layout changes: line breaks and indentation between tags, and extra spaces inside tags (<a id = "1" > becomes <a id="1">). Everything else is copied as written:

  • Text is never re-indented or trimmed. An element with text and child elements (mixed content, like <p>Hi <b>you</b>!</p>) keeps its whole inside as it is, because the spaces there are part of the text.
  • xml:space="preserve" works the same way: the element and everything inside it stay as written.
  • Attributes keep their order, their quotes and their values, including line breaks.
  • Entity and character references like &amp; and &#169; stay references; nothing is decoded.
  • Comments, CDATA sections, processing instructions, the DOCTYPE and the <?xml …?> declaration are kept, also by Minify.
  • <br/> stays self-closing and <item></item> stays written out. An element with only spaces inside (<a> </a>) keeps them.

Fix common mistakes in one click

XML copied out of an HTML page or a chat answer, typed by hand or cut off in the middle often isn't quite well-formed. Fix it repairs the usual mistakes and changes nothing else: your layout, text and attributes stay exactly as you wrote them. For example, this input:

<menu>
  <dish id=12 spicy=no>
    <name>Fish & Chips</name>
    <price>9.50&nbsp;EUR</price>
    <note>Ready in < 15 minutes</note>
  </dish>

Try it with this example: it opens in the tool above, and Fix all 6 shows up under it. It becomes this, and the note lists the 6 changes (quotes around 2 attribute values, the &, the &nbsp;, the < and the missing </menu>):

<menu>
  <dish id="12" spicy="no">
    <name>Fish &amp; Chips</name>
    <price>9.50&#160;EUR</price>
    <note>Ready in &lt; 15 minutes</note>
  </dish></menu>

The missing end tag goes at the very end, so nothing else moves; press Format to give it a line of its own.

What it fixes:

  • a stray & (AT&T, ?page=2&sort=name) becomes &amp;
  • HTML entities XML doesn't know (&nbsp;, &copy;, &eacute; and the rest of HTML's list) become their character numbers, like &#160;, unless the document has a DTD that could declare them
  • a < that can't start a tag (a < b, x <= 3, 5<10) becomes &lt;
  • attribute values without quotes (id=12) get quotes
  • tags still open at the end, because the XML was cut off or the last end tags were forgotten: the missing end tags are added at the end, innermost first
  • spaces or empty lines before <?xml …?>, and a ``` code fence around the whole XML, are removed

If something else is wrong, like tags closed in the wrong order, a duplicate attribute or two root elements, Fix it doesn't guess: the message shows you where, and you decide.

Common XML errors

Here's what the formatter says about the mistakes we see most, and how to fix each one.

  • An & on its own: <company>AT&T</company>

    It says Hmm, something's off on line 1, column 12: a “&” must start an entity like &amp; (ending with ;). To write a plain &, use &amp; 🤔

    Fix: Write AT&amp;T. Fix it does this for you.

  • HTML entities: <p>a&nbsp;b</p>

    It says Hmm, something's off on line 1, column 5: unknown entity &nbsp;. XML only knows &amp; &lt; &gt; &quot; &apos;, so use a number like &#160; instead 🤔

    Fix: Write the no-break space as a number, &#160; (XML has only five named entities). Fix it does this for you.

  • A < in text: <price>5 < 10</price>

    It says Hmm, something's off on line 1, column 10: a “<” in text must be written as &lt; 🤔

    Fix: Write 5 &lt; 10, or wrap the text in <![CDATA[ … ]]>. Fix it writes the &lt; for you.

  • Tags closed in the wrong order: <a><b>text</a></b>

    It says Hmm, something's off on line 1, column 11: <b> from line 1 isn't closed before </a>. Add </b> (or write <b/> if it's empty) 🤔

    Fix: Close the inner tag first: <a><b>text</b></a>.

  • HTML tags that are never closed: <p>Line one<br>Line two</p>

    It says Hmm, something's off on line 1, column 24: <br> from line 1 isn't closed before </p>. Add </br> (or write <br/> if it's empty) 🤔

    Fix: In XML every tag is closed: write <br/>.

  • Attribute values without quotes: <item id=7/>

    It says Hmm, something's off on line 1, column 10: attribute values need quotes: id="…" 🤔

    Fix: Write <item id="7"/>. Fix it adds the quotes for you.

  • Anything before the declaration: a space before <?xml version="1.0"?>

    It says Hmm, something's off on line 1, column 2: <?xml …?> must be the very first thing, with nothing before it (not even a space or an empty line) 🤔

    Fix: Delete everything before it, empty lines too. Fix it removes spaces and empty lines for you.

  • Two root elements: <a/> and then <b/> on the next line

    It says Hmm, something's off on line 2, column 1: a second root element, <b>. XML allows only one, so wrap them all in one element like <root>…</root> 🤔

    Fix: Put both inside one element.

FAQ

Is my XML uploaded anywhere?

No. It runs in your browser, including opening files: a file you open or drop is read right here, and we never upload, log or store your XML. The XML Formatter doesn't remember your input either: reload the page and it's gone. See the privacy page for details.

Does formatting change my data?

No. Only the layout between tags changes. Text, attribute values, references, comments and CDATA are copied exactly as written, and an element that mixes text with tags (a paragraph with a bold word in it) keeps its inside untouched, so its text never gets extra spaces or line breaks. A program that reads the XML sees the same elements, attributes and text.

Can it fix invalid XML automatically?

Yes, the common mistakes: press Fix it below the error message. It escapes a stray & or <, turns HTML entities like &nbsp; into character numbers, adds quotes around attribute values and adds the end tags missing at the end. Every change is listed, nothing else is touched, Keep accepts it and Undo brings your original back. It only offers a fix when it's sure what you meant: tags closed in the wrong order, for example, are yours to sort out. Try it with an example.

What does “well-formed” mean? Does it check my schema?

Well-formed means the XML follows the syntax rules every XML parser needs: one root element, every tag closed in the right order, quoted attribute values, and & and < written as &amp; and &lt; in text. That's what this tool checks. It doesn't validate against a DTD or an XSD schema, so any element and attribute names are fine.

Do I need to declare namespace prefixes like soap:?

Not here: soap:Body is checked as a plain name, so a snippet copied out of a bigger document formats fine. Most programs that read XML (browsers too) do need the declaration, like xmlns:soap="…" on that element or one around it, so keep it in complete documents.

Can I use entities like &nbsp; or &copy;?

XML knows only &amp;, &lt;, &gt;, &quot; and &apos;. Write other characters directly (©) or as a number (&#169;, &#xA9;); Fix it does that for you. Entities declared in the document's own DOCTYPE work too. If the DOCTYPE points to an outside DTD file (SYSTEM or PUBLIC), we can't see its declarations, so any entity name is accepted there.

What's the difference between Format and Minify?

Format puts each tag on its own line and indents it so people can read it. Minify removes the line breaks and spaces between tags so the file is as small as possible. Both keep comments and leave text alone.

How big can my XML be?

Files up to 20 MB open fine; inputs over 1 MB are checked and formatted in the background so the page stays responsive. Syntax colors switch off above 100 KB to keep typing smooth.