XML Beautifier
Pretty print or minify XML with comments, CDATA and escaping preserved, see well-formedness errors as you type, and run XPath 1.0 queries in the browser.
The interactive tool loads here when JavaScript runs.
About this toolWhat is the XML Beautifier?
The XML Beautifier checks that your XML is well-formed as you type, then pretty-prints it with two-space indentation or minifies it onto one line. A separate XPath Query panel evaluates expressions such as //book[@id='1']/title or count(//book) against the same document.
Parsing uses the browser's own DOMParser, the same XML parser that loads SVG and RSS. Output is written back with proper escaping, so &, < and " stay escaped in text and attribute values. Comments, CDATA sections, processing instructions and the DOCTYPE are kept, and the XML declaration at the top is carried over.
Formatting never adds whitespace where it would change your data. Elements that hold text, including mixed content like <p>Hello <b>world</b>!</p>, are printed exactly as they were, and xml:space="preserve" is respected.
- Live well-formedness check with the parser's own message
- Beautify and minify with correct escaping
- Keeps comments, CDATA, processing instructions and DOCTYPE
- Respects mixed content and xml:space="preserve"
- XPath 1.0 queries using the browser's evaluator
- Runs in your browser; documents are not uploaded
Updated
How to use the XML Beautifier
- Paste XML into the Input XML box, or click Load Sample to start from a small book catalogue.
- Watch the badge: it shows whether the document is well-formed, and the parser message appears below the input if it is not.
- Click Beautify for indented output or Minify to remove whitespace between elements.
- Copy the result or download it as formatted.xml with the buttons above the output.
- To extract data, type an XPath expression in the XPath Query panel and click Query. Matching elements, text values, numbers or booleans appear under Query Result.
Worked examples
Escapes, CDATA and comments survive
Input
<?xml version="1.0"?><r><!-- c --><a t="x & y">1 < 2</a><s><![CDATA[a < b]]></s></r>
Output
<?xml version="1.0"?> <r> <!-- c --> <a t="x & y">1 < 2</a> <s><![CDATA[a < b]]></s> </r>
The escaped ampersand and less-than sign stay escaped, so the output is still well-formed. The comment gets its own line and the CDATA section is copied exactly.
Counting with XPath
Input
Load Sample, then query: count(//book[price > 13])
Output
1
XPath compares the text of <price> as a number. Only To Kill a Mockingbird, at 14.99, is above 13.
Background and insight
XML 1.0 became a W3C Recommendation in February 1998 and is still the format behind SOAP, SAML assertions, Maven POMs, Android layouts, Office files, RSS and sitemaps. Its rules are strict on purpose: one root element, every tag closed, attributes quoted, and & and < escaped in text. A document that breaks any of them is not well-formed, and a conforming parser must stop rather than guess. That is why the badge next to the input turns red on a single missing end tag.
Whitespace is the subtle part. To an XML parser the newlines and spaces between elements are text nodes like any other. Indenting a document that contains only nested elements is safe in practice, because consumers ignore that whitespace. Indenting inside an element that mixes text and child elements is not, because the added spaces become part of the content. The XML spec gives authors xml:space="preserve" to say "do not touch whitespace here", and this tool honours it.
XPath 1.0 (1999) is the query language built into browsers. The gotcha everyone hits is namespaces: in a document with a default namespace, such as xmlns="http://www.sitemaps.org/schemas/sitemap/0.9", //url matches nothing because the elements are in that namespace. Use //*[local-name()='url'] instead.
Common use cases
Reading SOAP and SAML payloads
Expand a one-line SOAP response or decoded SAML assertion to see its structure and attributes.
Checking sitemaps and feeds
Confirm that a sitemap.xml or RSS feed is well-formed before submitting it, and count entries with XPath.
Editing build and config files
Re-indent a Maven pom.xml, .csproj or Android layout after a merge left the indentation mixed.
Shrinking fixtures
Minify XML test fixtures without touching text content, CDATA or preserved whitespace.
Tips and limitations
- Text inside leaf elements is never trimmed. If <name> Ada </name> has spaces in the source, they are still there in the output, because to XML they are data.
- The XML declaration is copied from your input as written. If you omit it, none is added.
- Attribute order is kept, but quotes are normalised to double quotes and values are re-escaped.
- For namespaced documents, query with local-name(), for example //*[local-name()='loc'], because the browser's XPath has no way to bind a prefix to the default namespace here.
- Well-formed is not the same as valid. This tool does not check against an XSD or DTD.
Sources and further reading
- Extensible Markup Language (XML) 1.0 (Fifth Edition), W3C. Well-formedness rules and the xml:space attribute
- XML Path Language (XPath) Version 1.0, W3C. The 1999 query language used by the XPath panel
- Namespaces in XML 1.0 (Third Edition), W3C. How default namespaces apply to element names
- Sitemaps XML format, Google, Yahoo and Microsoft. The sitemap schema and its default namespace
Frequently asked questions
Why does my XML show as invalid when it opens fine in a browser tab?
Browsers often fall back to rendering XML as HTML or plain text, which hides errors. A strict XML parser stops at the first problem: an unescaped &, an unclosed tag, or a second root element. The message under the input tells you which one and where.
Does formatting XML change its meaning?
For element-only content, no: the whitespace added between child elements is ignored by practically every consumer. This tool adds no whitespace inside elements that contain text or that set xml:space="preserve", which are the cases where it would matter.
Why does my XPath query return no matches?
Usually because of a default namespace. If the root element has xmlns="...", every element is in that namespace and //book will not match. Rewrite the query as //*[local-name()='book'] and it will.
Are CDATA sections kept when I beautify?
Yes, CDATA sections are written back exactly as they appear, including any < or & characters inside them, so the content does not get re-escaped.
Can I minify XML without breaking it?
Minify removes whitespace-only text between elements and nothing else. Text content, CDATA, comments and anything under xml:space="preserve" are left alone, so the minified file carries the same data.
Does the XML Beautifier validate against an XSD schema?
No. It checks well-formedness, meaning the document follows XML syntax. Validation against an XSD or DTD needs a schema-aware tool such as xmllint with the --schema option.
Is there an in-depth guide to the XML Beautifier?
Yes. “Formatting HTML, XML and YAML Without Changing What They Mean” covers the background, how it works, common pitfalls and worked examples. Read the full guide.
Related Tools You May Like
Free, in-browser tools that pair well with this one.