How to Format and Validate XML Online

3 min read

Formatting XML means rewriting it with consistent indentation and line breaks so its tree structure is visible. The data doesn't change. A formatter also acts as a validator: it has to parse the document first, so malformed XML fails with an error message instead of producing output.

Why format XML?

XML often arrives on one line: an API response, a SOAP envelope, a minified config file, or a feed. This is valid but unreadable:

<order id="1042"><customer><name>Ana Silva</name><email>ana@example.com</email></customer><items><item sku="A-7" qty="2"/><item sku="B-3" qty="1"/></items></order>

Formatted, the nesting is obvious:

<order id="1042">
  <customer>
    <name>Ana Silva</name>
    <email>ana@example.com</email>
  </customer>
  <items>
    <item sku="A-7" qty="2"/>
    <item sku="B-3" qty="1"/>
  </items>
</order>

Readable structure makes it easy to spot a missing element, a value in the wrong parent, or an unexpected attribute.

How to format XML with the tool

  1. Open the XML Formatter.
  2. Paste your XML into the input pane, or click Sample to try it first.
  3. Choose Format and pick an indent: 2 spaces, 4 spaces, or tabs.
  4. Copy the result, or click Download to save it as a file.

Parsing uses the browser's built-in DOMParser, so the XML stays on your device. That matters when the payload contains customer data or credentials.

Minify for transport

Switch to Minify to do the opposite: strip whitespace and comments to get the smallest possible file. Use minified XML when sending data over a network, and formatted XML when you're reading or editing it.

The errors a formatter catches

When the document is malformed, you get an error instead of output. The usual causes:

Unclosed tags. Every opening tag needs a closing tag, or must close itself:

<item sku="A-7">      <!-- wrong: never closed -->
<item sku="A-7"/>     <!-- right -->

Mismatched names. XML is case-sensitive, so <Name> and </name> don't match.

Unescaped special characters. A bare & or < inside text breaks parsing. Write &amp; and &lt; instead:

<company>Smith & Sons</company>      <!-- invalid -->
<company>Smith &amp; Sons</company>  <!-- valid -->

The HTML Entity Encoder converts these characters in bulk.

Unquoted attributes. <item qty=2> is valid in lenient HTML parsers, never in XML. Use qty="2".

More than one root element. An XML document has exactly one top-level element. Two sibling roots need a wrapper.

Formatting vs. validating against a schema

A formatter checks that XML is well-formed: the syntax rules above. It doesn't check that the document is valid against a schema (XSD or DTD), such as whether <order> must contain <customer>. For schema validation, use your IDE or a validator that loads the schema.

Where you'll meet XML

  • Configuration: Maven pom.xml, Android layouts, Spring, .NET config files.
  • Feeds: RSS and Atom.
  • Office files: DOCX and XLSX are ZIP archives full of XML.
  • Integrations: SOAP web services and many enterprise APIs.
  • Sitemaps: the sitemap.xml search engines read.

Summary

Paste into the XML Formatter, choose Format or Minify, and read any error message as a pointer to the broken tag, character, or attribute. For converting between data formats, see the YAML to JSON and CSV to JSON converters.