XML Formatter

Beautify or minify XML, and find out where it stops being well-formed. It works on any format that is XML underneath: SOAP responses, RSS feeds, sitemaps, Android layouts, a hand-edited SVG. Your browser's own XML parser does the checking. Indent with 2 spaces, 4 spaces or a tab, and switch between them at any time.

XML Input
Formatted XML

1 XML Formatter example

One long line

1
<?xml version="1.0" encoding="UTF-8"?><catalog><!-- autumn list --><book id="1" lang="en"><title>The Left Hand of Darkness</title><author>Ursula K. Le Guin</author><price currency="USD">9.95</price></book><book id="2" lang="en"><title>The Pragmatic Programmer</title><author>Andrew Hunt</author><price currency="USD">32.00</price></book></catalog>

Formatted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <!-- autumn list -->
  <book id="1" lang="en">
    <title>The Left Hand of Darkness</title>
    <author>Ursula K. Le Guin</author>
    <price currency="USD">9.95</price>
  </book>
  <book id="2" lang="en">
    <title>The Pragmatic Programmer</title>
    <author>Andrew Hunt</author>
    <price currency="USD">32.00</price>
  </book>
</catalog>

2 Well-formed is not the same as valid

This checks well-formedness, not validity. Well-formed means every tag is closed and correctly nested, attributes are quoted, there is exactly one root element, and no bare & or < appears in text. Every XML parser enforces that before it does anything else.

Validity means the document also matches a DTD or an XSD, with the right elements in the right places. A document can be well-formed and still wrong for its schema. That check needs a validating parser such as xmllint with the schema file; this tool does not do it.

3 What formatting keeps

Text content

Characters are kept, but leading and trailing whitespace inside an element is trimmed. In mixed content, each text run moves onto its own line, so keep the original if inline spacing matters.

CDATA sections

Emitted verbatim. Escaping the contents would change what the section means.

Comments and the declaration

Both preserved, along with any processing instructions.

Namespaces

Prefixes and xmlns declarations pass through unchanged, so SOAP envelopes and namespaced documents survive.

4 The ampersand

Check for a bare & first. It is the most common reason an XML document fails to parse, and it usually arrives in a URL with query parameters pasted straight into an element. ?a=1&b=2 has to be written ?a=1&amp;b=2, or wrapped in CDATA. The error message points at the ampersand.

How to use the XML Formatter tool

  1. Paste XML, or load a file with Upload XML. SVG, RSS, sitemaps and SOAP responses are all ordinary XML.
  2. Pick the indent width. The document reformats as you type.
  3. Format pretty-prints, and Minify removes the whitespace between elements and collapses runs of spaces.
  4. If the document is not well-formed, the error appears in red with the browser parser's own description of what it found.
  5. Choose Tab in the Indent menu to indent with tab characters instead of 2 or 4 spaces. The output rebuilds as soon as you change it.

How to format and validate XML in code

Command line

# xmllint ships with libxml2, so it is already on most systems
xmllint --format messy.xml -o pretty.xml

# Well-formedness only
xmllint --noout document.xml

# Validate against a schema, which is the stronger check
xmllint --noout --schema schema.xsd document.xml
xmllint --noout --dtdvalid doc.dtd document.xml

# Query it
xmllint --xpath "//book[price>100]/title/text()" catalogue.xml

Python

# pip install lxml
from lxml import etree

parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse("catalogue.xml", parser)

print(etree.tostring(tree, pretty_print=True, xml_declaration=True,
                     encoding="UTF-8").decode())

# Validate against an XSD
schema = etree.XMLSchema(etree.parse("schema.xsd"))
if not schema.validate(tree):
    for error in schema.error_log:
        print(f"line {error.line}: {error.message}")

remove_blank_text on the parser is what makes pretty_print work. Without it, lxml keeps the original whitespace and the output looks unchanged.

PHP

<?php
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->preserveWhiteSpace = false;   // required for formatOutput to do anything
$dom->formatOutput = true;

libxml_use_internal_errors(true);
if (!$dom->loadXML($xml)) {
    foreach (libxml_get_errors() as $e) {
        echo "line {$e->line}: " . trim($e->message) . PHP_EOL;
    }
    libxml_clear_errors();
    exit(1);
}

echo $dom->saveXML();

Common errors and how to fix them

EntityRef: expecting ';'
A bare & in text. Write it as &amp;, or wrap the value in CDATA. The cause is usually a URL with query parameters pasted straight in.
Opening and ending tag mismatch
Tags are not nested correctly, or one is unclosed. XML has no optional closing tags, so even an empty element needs <br/> rather than <br>.
Extra content at the end of the document
There is more than one root element. XML allows exactly one, so wrap the others in a container.
It parses but my program still rejects it
Well-formed is not the same as valid. The document is syntactically fine but does not match its DTD or XSD. Validate with xmllint --schema to see which rule it breaks.
Accented characters came out as question marks
The declared encoding does not match the bytes. Check that the file is saved as UTF-8 and that the declaration says so.

Good to know

Disable external entities in any XML parser on a server. A document can declare an entity that reads /etc/passwd or fetches a URL, and a parser with default settings may resolve it. That attack is XXE (XML external entity injection), and it still turns up in security reviews. Use libxml_disable_entity_loader in older PHP, resolve_entities=False in lxml, and the equivalent setting in whatever parser you use.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Frequently Asked Questions

Does it check that my XML is valid?

It checks that the XML is well-formed: every tag closed, attributes quoted, one root element, entities escaped. It does not validate against a DTD or an XSD, which is a separate and much stricter check.

Is whitespace inside elements preserved?

Mostly. Format keeps the characters but trims leading and trailing whitespace inside each element, and puts each text run in mixed content on its own line. Minify removes the whitespace between tags and collapses runs of spaces. If a document depends on exact mixed-content spacing, keep the original.

Are comments and CDATA kept?

Yes, both, along with the XML declaration and any processing instructions. CDATA sections are written out verbatim, since escaping their contents would change the meaning.

Can it handle SVG, RSS and sitemaps?

Yes. Those are all ordinary XML, and Upload XML accepts .svg, .rss and .xsd files as well as .xml. Formatting a hand-edited SVG is a common use.

Why does it complain about an ampersand?

A bare & is not valid XML; it has to be &amp;. It usually appears when a URL with query parameters is pasted straight into an element, and it is exactly the error a well-formedness check exists to catch.

Should XML be indented with tabs or spaces?

The parser does not care: whitespace between elements is ignored by almost every consumer, so it is a convention choice. Match the file you are editing or your project's editor settings. Android and Maven files usually use four spaces, most web projects two, and tabs produce a slightly smaller file that each editor can display at its own width. Pick 2 Spaces, 4 Spaces or Tab in the Indent menu.

Can I change XML indentation from spaces to tabs?

Yes. Paste the document, pick Tab under Indent, and the output is rebuilt from the parsed tree, so the original indentation is discarded completely rather than patched. The same works in reverse, or from 2 to 4 spaces. Text inside elements and CDATA sections are not touched, so only the structural whitespace changes. Minify ignores the setting and removes the indentation entirely.

Related

To work with the data rather than the markup, XML to JSON converts it and JSON to XML goes back. For HTML specifically, the HTML formatter is more forgiving about unclosed tags.

More Code Formatters & Beautifiers

Browse all code formatters & beautifiers

Copied to clipboard!