XML Formatter

Format, validate, and beautify XML documents with syntax highlighting and error detection

Runs locally

XML Input

Formatted Output

Inspector

Paste well-formed XML — structure, tree and element paths appear here automatically

Advanced Options

Compare with another XML

Send this document to Data Diff and paste a second version to see what changed.

Validation with the exact error position

Validation runs on every keystroke using the browser XML parser. Well-formed documents get live counts of elements, attributes, text nodes and depth. Broken ones report the exact line and column, a probable cause, and a highlight at the failing position — not just "not well-formed".

Common XML errors this tool pinpoints

These mistakes account for most invalid XML. Each one is detected with a position and a probable cause:

Mismatched tags

<person> closed with </perso>. The parser reports "Opening and ending tag mismatch" with the line of both tags; the highlight jumps to the failing one.

Unclosed tag

A missing > or a missing end tag. Usually reported as "expected >" or "premature end of data in tag X" at the exact position.

Duplicate attribute

The same attribute name twice on one element, e.g. <item id="1" id="2">. Reported as "Attribute redefined" pointing at the second occurrence.

Invalid entity / raw &

A literal & or < inside text must be escaped (&amp;, &lt;). The parser reports "EntityRef: expecting ;" or "not well-formed" at the exact character.

Before / After

Minified API response on the left, the same document formatted with 2-space indent on the right. Structure, tree and paths update from the parsed document.

Before
<catalog><book id="1" category="fiction"><title>The Hobbit</title><author>J.R.R. Tolkien</author></book><book id="2" category="tech"><title>Refactoring</title><author>Martin Fowler</author></book></catalog>
After
<catalog>
  <book id="1" category="fiction">
    <title>The Hobbit</title>
    <author>J.R.R. Tolkien</author>
  </book>
  <book id="2" category="tech">
    <title>Refactoring</title>
    <author>Martin Fowler</author>
  </book>
</catalog>

XML quick reference (W3C XML 1.0)

  • Well-formed ≠ valid: well-formed means tags nest and close correctly; valid additionally means the document follows a DTD or XSD schema.
  • Five predefined entities: &lt; &gt; &amp; &quot; &apos; — a literal < or & in text is always an error.
  • Element and attribute names are case-sensitive and may carry namespace prefixes (ns:tag).
  • Exactly one root element; an XML declaration like <?xml version="1.0" encoding="UTF-8"?> must come first if present.
  • Comments <!-- … --> may not contain -- and may not be nested.

Next steps with this data

Formatting is the first step. Continue the workflow without leaving your browser:

Frequently Asked Questions

Common questions about Xml Formatter formatting answered

The formatter performs well-formedness checking and basic validation. It checks proper tag nesting, attribute syntax, and XML declaration compliance following W3C XML 1.0/1.1 specifications.

Yes, the built-in XPath tester lets you write and evaluate XPath 1.0 and 2.0 expressions against your XML document, highlighting matching elements and showing results instantly.

Yes, the formatter correctly processes XML documents with namespaces, preserving namespace declarations and prefixes while formatting elements and attributes properly.

Yes, the conversion feature lets you transform XML to JSON and back with intelligent mapping that preserves element structure, attributes, and data types.

The XML formatter supports files up to 50MB with optimized parsing algorithms, making it suitable for large configuration files, SOAP messages, and data interchange documents.

Namespace declarations (xmlns, xmlns:prefix) are treated as ordinary attributes for indentation purposes and are never reordered or dropped, so namespace URIs stay bound to the same prefixes. Formatting never resolves or rewrites prefixes - tools downstream resolve them per the W3C Namespaces in XML recommendation.

Formatting only re-indents and normalizes whitespace between elements. Validation checks structure: balanced tags, one root element, correct attribute quoting, and entity syntax per the W3C XML 1.0 specification. The formatter reports well-formedness errors (malformed input cannot be re-indented) but does not validate against a DTD or XSD schema.

Yes - CDATA blocks keep their exact inner content (that is their purpose: escaping text that would otherwise need entity encoding), and comments pass through unchanged apart from surrounding indentation. XML declarations and processing instructions like <?xml version="1.0"?> also stay at the top untouched.

{-- * External Resources Component(#19 Phase 3b 内容佐证工程) * 工具页「权威引用」区块:RFC / W3C / WHATWG / ECMA / IANA / 官方规范站 / 官方语言文档 / Wikipedia。 * * - 接受 :slug 属性 → 经 config/tool-sources.php 家族矩阵渲染该工具的权威引用 * - slug 未命中映射时不渲染(无权威来源的工具静默跳过) * - 链接 title 保持英文(引用源专名);description 经 * common.resources.descriptions.{key} 本地化,lang 未命中回退英文(线上不裸奔) * - 链接保持 dofollow(rel="noopener noreferrer") * * @param string|null $slug --}}

Related Tools