What the XML Formatter does
The XML Formatter checks that your XML is well-formed and then indents it with two spaces per level, putting each element on its own line so the structure is easy to follow. It works for any XML document, including SOAP envelopes, WSDL service descriptions, RSS feeds and configuration files that arrive as one long line.
XML Formatter examples
The built-in sample
Input
<users><user id="1"><name>Ada</name></user><user id="2"><name>Lin</name></user></users>Output
<users>
<user id="1">
<name>Ada</name>
</user>
<user id="2">
<name>Lin</name>
</user>
</users>This is the input that the Load sample button fills in.
A SOAP envelope
Input
<?xml version="1.0"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Header/><soap:Body><m:GetPrice xmlns:m="urn:shop"><m:Item>Pen</m:Item></m:GetPrice></soap:Body></soap:Envelope>Output
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header/>
<soap:Body>
<m:GetPrice xmlns:m="urn:shop">
<m:Item>Pen</m:Item>
</m:GetPrice>
</soap:Body>
</soap:Envelope>SOAP messages and WSDL files are ordinary XML, so the same formatter handles them. Namespace prefixes and declarations are left exactly as written, and the XML declaration stays on its own first line.
Each output was produced by running this tool on the input above it.
How it works
The document is first checked for well-formedness with the browser's XML parser. Whitespace between tags is then normalised and every element is placed on its own line, indented two spaces per level of nesting. Elements that only contain text stay on one line, and comments and processing instructions are kept in place.
Details and limits
- The text is re-indented, not rebuilt from a parsed tree: attribute order, quote style, entities, comments and the XML declaration stay exactly as you wrote them.
- An element that contains only text, or text mixed with inline elements, is kept on one line:
<p>Some <b>bold</b> text</p>is not split. - A CDATA section that directly follows a tag is moved to its own line. That adds whitespace around it, which matters if the receiving system does not trim.
- A
>written unescaped inside an attribute value confuses the indentation of the lines after it. Write>in attribute values. xml:space="preserve"is not honoured: whitespace between tags is always normalised.- Input that is not valid XML stops the tool, and the status line shows where the problem is. The XML Validator page lists the usual causes.
Questions
Can it format SOAP messages and WSDL files?
Yes. Both are XML documents, so they are checked and indented like any other. Namespace prefixes are not changed.
Does formatting change the document?
It changes whitespace between tags only. In data-oriented XML that whitespace has no meaning. In mixed content, and next to CDATA sections, it can be significant, so check those cases.