Skip to content

↑↓ navigate ↵ open Esc close Search runs locally

XML Formatter & Validator (with XPath Tree View)

Format, validate and minify XML in your browser, with exact error locations, comments and CDATA kept as written, and a tree view that copies the XPath of any element.

  • Runs in your browser
  • Nothing is uploaded
  • Free, no sign-up
0 B
Runs in your browser. Nothing is uploaded. Tip: click any element in the Tree view to copy its XPath

How to use the XML Formatter tool#

  1. Paste XML into the left box, or drop an .xml, .svg, RSS or sitemap file onto it. Small inputs are checked as you type; for large files press Format.
  2. Choose Format (2 spaces, 4 spaces or tabs) or Minify. Tick One attribute per line for long tags, or Self-close empty tags to write <tag></tag> as <tag/>.
  3. If the XML is not well-formed, the message under the tool gives the line, column and a preview. Press Show me to jump to the problem.
  4. Switch the output to Tree to explore the document. Click an element, attribute or text to see and copy its XPath, such as /urlset/url[2]/loc.

Examples#

Pretty-print minified XML

Input
<?xml version="1.0" encoding="UTF-8"?><catalog><book id="bk101" lang='en'><title>XML &amp; You</title><price currency="USD">44.95</price><tags/></book></catalog>
Output
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <book id="bk101" lang='en'>
    <title>XML &amp; You</title>
    <price currency="USD">44.95</price>
    <tags/>
  </book>
</catalog>

Two-space indentation. The single quotes around en, the &amp; entity and the empty <tags/> stay exactly as written.

Find a mismatched tag

Input
<note>
  <to>Ali</to>
  <from>Sara</frm>
</note>
Output
✗ Line 3, column 13: Expected closing tag 'from' (opened in line 3, col 3) instead of closing tag 'frm'.

XML tag names are case-sensitive and every opening tag needs a matching closing tag. The most common XML error.

Keep mixed content, comments and CDATA

Input
<doc><p>This is <b>bold</b> and <i>italic</i> text.</p><note><!-- reviewed --><![CDATA[if (a < b && c > d) {}]]></note></doc>
Output
<doc>
  <p>This is <b>bold</b> and <i>italic</i> text.</p>
  <note><!-- reviewed --><![CDATA[if (a < b && c > d) {}]]></note>
</doc>

Elements that mix text and tags, and CDATA sections, are kept exactly as written, so no space is added to or removed from the text.

One attribute per line (SVG)

Input
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M12 2 2 22h20z" fill="none" stroke="currentColor"></path></svg>
Output
<svg
  xmlns="http://www.w3.org/2000/svg"
  width="24"
  height="24"
  viewBox="0 0 24 24">
  <path
    d="M12 2 2 22h20z"
    fill="none"
    stroke="currentColor"/>
</svg>

Long tags become easy to read and to diff. Self-close empty tags turned <path ...></path> into <path .../>.

Minify a sitemap

Input
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://example.com/</loc>
    <lastmod>2026-11-30</lastmod>
  </url>
</urlset>
Output
<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://example.com/</loc><lastmod>2026-11-30</lastmod></url></urlset>

Whitespace between tags is removed. The text inside each element is not touched.

What is XML?#

XML (Extensible Markup Language) is a text format that stores data as a tree of named elements, such as <price currency="USD">44.95</price>, with attributes, text, comments and CDATA sections. It is defined in the W3C XML 1.0 specification and is still everywhere: sitemaps, RSS and Atom feeds, SVG images, SOAP APIs, Android layouts, Maven pom.xml files, Office documents and many configuration files.

Well-formed vs valid XML#

Well-formed XML follows the syntax rules: one root element, every tag closed in the right order, quoted attribute values and escaped special characters. Every XML parser rejects a document that is not well-formed. Valid XML is well-formed and also matches a schema (a DTD or an XSD) that says which elements and attributes are allowed. This tool checks that your XML is well-formed. It does not check it against a schema.

XML syntax rules#

RuleValidInvalid
Exactly one root element<a><b/><c/></a><b/><c/>
Every tag is closed<br/>, <p></p><br>
Tags close in reverse order<b><i>x</i></b><b><i>x</b></i>
Names are case-sensitive<Item></Item><Item></item>
Attribute values are quotedid="5", id='5'id=5, checked
The declaration comes first<?xml version="1.0"?> on line 1a blank line or text before it

Special characters and entities#

Five characters have a special meaning in XML. In text and attribute values, write them as entities:

CharacterEntityMust be escaped
&&amp;Always
<&lt;Always
>&gt;Only in ]]>, but common practice
"&quot;Inside attribute values in double quotes
'&apos;Inside attribute values in single quotes

HTML entities such as &nbsp; and &copy; do not exist in XML. Use numeric references instead (&#160;, &#169;), or put code and other text full of special characters in a CDATA section: <![CDATA[ a < b ]]>.

How this formatter is different#

Many formatters parse XML into objects and write it back, which can drop comments and the DOCTYPE, change attribute quotes, decode entities or trim the text inside elements. This tool reformats the original text instead, so comments, CDATA, the DOCTYPE, entities, attribute order and quote style stay exactly as written. Elements that mix text and tags, such as <p>Hello <b>you</b></p>, and elements with xml:space="preserve" are left untouched, because adding line breaks there would change the text. Only the whitespace between tags changes.

Your XML is checked twice: first by fast-xml-parser, which gives clear messages, then by your browser's own XML parser, which also catches problems such as undefined entities.

XPath basics#

XPath is a query language that points at parts of an XML document. The tree view gives you the exact path of any node, ready for your code or xmllint --xpath.

XPathSelects
/catalog/bookEvery book element directly under the root catalog
/catalog/book[2]The second book
/catalog/book/@idThe id attribute of each book
/catalog/book/title/text()The text inside each title
//priceEvery price element, at any depth
//book[@lang='en']Books whose lang attribute is en

Format and validate XML in code#

Command line (xmllint)#

bash
xmllint --format data.xml                    # formatxmllint --noout data.xml                     # check it is well-formedxmllint --noout --schema schema.xsd data.xml # validate against an XSD

PHP#

php
$dom = new DOMDocument();$dom->preserveWhiteSpace = false;   // set before loading$dom->formatOutput = true;$dom->loadXML($xml);                // false when the XML is not well-formedecho $dom->saveXML();

Python#

python
import xml.etree.ElementTree as ET tree = ET.parse('data.xml')ET.indent(tree, space='  ')        # Python 3.9+tree.write('formatted.xml', encoding='utf-8', xml_declaration=True)# Note: ElementTree drops comments and the DOCTYPE by default.

Common errors and fixes#

  • Expected closing tag 'from' ... instead of closing tag 'frm'

    A closing tag does not match the tag that is open. Check the spelling and the case (<Item> and </item> are different), and close tags in the reverse order they were opened.

  • Multiple possible root nodes found

    An XML document can have only one root element. Wrap everything in one element, such as <items>...</items>.

  • Entity 'nbsp' not defined or an error at an &

    Write & as &amp; in text and attribute values. HTML entities like &nbsp; are not part of XML: use &#160; instead.

  • XML declaration allowed only at the start of the document

    Something comes before <?xml ...?>, often a blank line printed by PHP before a sitemap or RSS feed. Remove any whitespace before <?php and after ?> in your files.

  • Attribute 'checked' is without value

    HTML allows attributes without a value, XML does not. Write checked="checked", and always quote attribute values.

Frequently asked questions#

Does formatting change my XML?

Only the whitespace between tags. Comments, CDATA, the DOCTYPE, entities, attribute order and quotes, and the text inside elements stay exactly as written. Elements that mix text and tags are not reformatted at all.

Does this tool validate XML against a schema (XSD or DTD)?

No. It checks that the XML is well-formed, which is what every parser requires. To validate against a schema, use xmllint --schema or a library in your language.

How do I find the error in my XML?

Paste it here. The message under the tool gives the line and column, shows the line with a caret under the problem, and Show me moves the cursor straight to it.

How do I use the XPath from the tree view?

Paste it into xmllint --xpath, PHP's DOMXPath, Python's lxml or any XPath tester. If the document has a default namespace (xmlns="...", as in sitemaps), most libraries need you to register a prefix for it first.

How large an XML file can I format?

Files up to 50 MB. Inputs over 1 MB are formatted when you press Format instead of on every keystroke, so typing stays fast.

Is my XML uploaded?

No. Validation, formatting and the tree view all run in your browser, so it is safe to paste API responses and configuration files.

Your next tool is one search away.

Free, private developer tools that run in your browser. No signup, nothing uploaded, no waiting.

Browse all tools

Tip: press Ctrl D to bookmark dev247.