XML Formatter
Indent and validate XML, including SOAP envelopes, RSS feeds and SVG markup.
XML Formatter tool
What this tool does
This formatter re-indents XML and checks that it is well-formed before it writes anything. An
element whose content is only text stays on one line, so a feed's
<title> is not spread across three, while nested elements are indented one
level per generation. Comments, CDATA sections, processing instructions and the DOCTYPE are
all preserved exactly as written.
When the document is not well-formed you get the line number and a caret under the problem, with both ends of the mismatch named: which tag was opened, on which line, and which tag tried to close it. Minify does the reverse — every byte of whitespace between tags removed, for embedding in a request body.
Common uses
- Reading a SOAP response that arrived as one 12,000-character line, where the fault string is buried somewhere in the middle.
- Checking an RSS or Atom feed before a validator sees it, or after hand-editing an entry.
- Tidying an SVG exported by a design tool so a diff shows the path that actually changed.
- Fixing a Maven
pom.xml, an Android layout or aweb.configwhose parser complains about line 1. - Minifying an XML payload before pasting it into a test fixture or a shell command.
A short example
This input:
<channel><title>Changelog</title><item><guid>bc-1</guid></item></channel> becomes:
<channel>
<title>Changelog</title>
<item>
<guid>bc-1</guid>
</item>
</channel> Well-formed is not the same as valid
XML uses those two words for different things, and only the first is checked here.
Well-formed means the syntax holds together: one root element, every tag
closed by a matching tag in the right order, attribute values quoted, and the reserved
characters escaped. Valid means the document also obeys a schema — a DTD, an
XSD or a RELAX NG grammar. A perfectly well-formed <invoice> is still
rejected by an XSD that requires a <total> child.
Three things break well-formedness more often than anything else:
-
A bare ampersand.
&must be written&— which is why URLs with query parameters so often break a feed. -
An unescaped
<in text content, usually from pasting code without a CDATA section. -
A namespace prefix that was never declared, such as
soap:Bodyin a fragment copied out of its envelope. The prefix is fine syntactically but a namespace-aware parser will refuse it.
Worth knowing
Indenting changes whitespace, and in XML whitespace inside an element is character data that
belongs to the document. For SOAP, RSS, config and most data XML nothing depends on it. It
matters in mixed content, where text and elements are siblings — reformatting
<p>see <b>this</b> page</p> can add spaces a renderer
shows — and anywhere xml:space="preserve" is set. Never reformat a signed
document: XML Signature hashes a canonical form of the bytes, and reindenting invalidates it.
Frequently asked questions
What does the error content is not allowed in prolog mean?
Something sits in front of the opening angle bracket: most often a UTF-8 byte order mark (EF BB BF), a stray blank line, or an HTML error page returned where XML was expected — check whether the document actually starts <!DOCTYPE html>. Java’s SAX parser reports the position as line 1, column 1 regardless of where the junk is. Delete the prefix rather than editing the markup.
The declaration says ISO-8859-1 but the file is UTF-8. Does that matter?
Yes, and it fails at the first accented character rather than at the top of the file, which makes it look like a content problem. A parser trusts the declaration, so a two-byte sequence read as Latin-1 becomes two characters and libxml2 answers Input is not proper UTF-8, indicate encoding. Either re-save in the declared encoding or correct the declaration; the hex view shows which bytes you really have.
Can I use this on HTML?
Only on XHTML-shaped markup. HTML allows <br> with no closing slash, unquoted attribute values and bare attributes such as disabled, none of which are well-formed XML. It also defines more than two thousand named entities, while XML predefines exactly five — &, <, >, " and ' — so one is an undefined-entity error.