XML Prettify
Pretty-print XML with clear indentation.
What is XML Prettify?
An XML formatter is a tool that parses an XML document and re-emits it with consistent two-space indentation, predictable line breaks around element boundaries, and aligned attribute layout so the source is readable, reviewable, and diff-friendly. The document tree never changes. Only the presentation does.
Backend developers, SOAP integration engineers, SEO specialists, and data analysts reach for an XML formatter when a sitemap exported from a CMS arrives on one line, when a SOAP response logged from a payment gateway needs to be inspected, or when an Android strings.xml resource file has drifted across contributors. A real parser also surfaces structural problems such as unclosed tags or invalid character references that text editors gloss over.
Why use an XML formatter?
- Make compressed XML reviewable. SOAP responses, RSS feeds, sitemaps, and Office Open XML documents often arrive without line breaks. Prettifying turns a single 50,000-character line into an inspectable tree.
- Catch structural errors immediately. A real XML parser refuses to format malformed documents. Missing closing tags, unbalanced namespaces, and invalid character references surface with the exact line and column where parsing failed.
- Standardize attribute layout. Long tags with five or six attributes are unreadable on one line. The formatter wraps attributes onto their own lines past a threshold, keeping short tags compact.
- Make diffs meaningful. Whitespace noise from text-editor copy-paste vanishes after formatting, so a pull request diff against an Android resource file or a Spring XML config shows only the changes that matter.
- Preserve namespace prefixes. Documents that use multiple XML namespaces keep every prefix intact, which is critical for SAML assertions, SOAP envelopes, and SVG with embedded metadata.
How to use the XML Prettify tool
- Paste your XML into the Input panel. Full documents with
<?xml ?>declarations and root-only fragments both work. - Click Prettify. The document is parsed with the browser’s native DOMParser and re-emitted with two-space indentation.
- Read the rewritten XML in the Output panel. Parse errors appear above the input with the line and column of the first issue.
- Click Copy to copy the formatted XML to your clipboard, then paste it into your sitemap, SOAP request file, or resource file.
XML formatting rules
The XML formatter applies the conventions in the W3C XML 1.0 specification and follows the canonical formatting style adopted by Eclipse, IntelliJ, and Visual Studio for XML files.
| Element | Convention | Why |
|---|---|---|
| Indentation | 2 spaces per level | Matches most IDE defaults |
| XML declaration | Preserved at top of document | Required for documents with non-UTF-8 encoding |
| Tag case | Preserved | XML is case-sensitive; renaming would break the document |
| Self-closing elements | Preserved (<br/> stays <br/>) | Required for XML, unlike HTML |
| Attribute quoting | Double quotes | Most common convention; single quotes also valid |
| Attribute order | Preserved | Avoids breaking documents that rely on attribute position |
| Whitespace inside leaf elements | Preserved | Required because mixed content is semantically significant |
| CDATA sections | Preserved verbatim | Required by the XML spec |
| Processing instructions | Preserved | <?xml-stylesheet ?> and similar PIs stay in place |
| Comments | <!-- --> preserved in position | Including DOCTYPE-internal comments |
| Trailing newline | Single newline at file end | POSIX text file convention |
Examples
A SOAP response logged on one line:
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><GetUserResponse xmlns="http://example.com/api"><User><Id>42</Id><Email>jane@example.com</Email><Roles><Role>admin</Role><Role>billing</Role></Roles></User></GetUserResponse></soap:Body></soap:Envelope>After prettifying, the envelope structure is indented and each element is on its own line:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetUserResponse xmlns="http://example.com/api">
<User>
<Id>42</Id>
<Email>jane@example.com</Email>
<Roles>
<Role>admin</Role>
<Role>billing</Role>
</Roles>
</User>
</GetUserResponse>
</soap:Body>
</soap:Envelope>XML Prettify vs alternatives
The right XML formatter depends on whether you need a single ad-hoc cleanup or repeatable formatting across a directory of XML files.
| Tool | Best for | Offline | Cost | Limitations |
|---|---|---|---|---|
| In-browser XML formatter | Single document pulled from a log or response | No | Free | No file watch, no save-on-format |
xmllint --format (libxml2) | CLI use in scripts and pipelines | Yes | Free | Requires libxml2; less forgiving of malformed input |
prettier --parser xml (community plugin) | Project-wide formatting alongside other languages | Yes | Free | Plugin maintenance varies |
| IntelliJ “Reformat Code” | Editing XML inside an IDE | Yes | Paid (IntelliJ) | Locked to the IDE |
For one-off cleanup of a sitemap or SOAP capture, an in-browser XML formatter is the fastest path. For project-wide consistency, wire xmllint --format or the Prettier XML plugin into a pre-commit hook.
Common use cases
- Backend developers inspecting SOAP request and response payloads logged from a payment gateway or legacy enterprise API.
- SEO specialists auditing XML sitemaps exported from WordPress, Yoast, or a custom CMS before submitting them to Google Search Console.
- Android developers standardizing
strings.xml,colors.xml, andlayoutresource files across a multi-module project. - Integration engineers working with EDI, ebXML, and HL7 v3 documents where structural validity is critical.
- SAML implementers debugging identity provider assertions and metadata documents during single sign-on troubleshooting.
Frequently asked questions
Does this XML formatter validate against a schema?
No. The formatter checks that the document is well-formed XML. It does not validate against XSD, DTD, or Relax NG schemas. For schema validation, use xmllint --schema or an IDE plugin that loads your schema file.
Why is my XML failing to parse?
The most common causes are unescaped &, <, or > characters in element content (use &, <, >), mismatched opening and closing tags, missing closing tags on void elements (XML requires <br/>, not <br>), and stray Byte Order Marks copied from a Windows editor. The error message points to the line and column where parsing breaks.
What is the difference between XML and HTML?
HTML is forgiving of unclosed tags and tolerates mixed casing. XML is strict: every tag must close, case matters, and certain characters must be escaped. The same source can be valid HTML but invalid XML. XHTML was the bridge between the two and is now largely deprecated.
Will the formatter preserve namespaces?
Yes. Namespace declarations (xmlns="..." and xmlns:prefix="...") and prefixed element and attribute names are preserved verbatim. The formatter does not collapse or rewrite namespace prefixes, which matters for SOAP, SAML, and SVG documents.
Does it preserve CDATA sections?
Yes. <![CDATA[...]]> blocks are preserved exactly as written, including all internal whitespace. This is critical for documents that embed script, SQL, or pre-formatted text.
How does it handle whitespace inside elements?
Whitespace inside leaf elements is preserved because XML treats it as semantically significant. Mixed content (an element containing both text and child elements) is also preserved verbatim to avoid changing the document’s meaning.
Can it format an XML sitemap?
Yes. Sitemap files following the sitemaps.org 0.9 schema parse cleanly. Each <url> and its children are indented predictably, making the sitemap easy to review for missing or duplicate URLs.
Does it work on Office Open XML files?
Office Open XML formats (.docx, .xlsx, .pptx) are ZIP archives containing many XML files. Extract the XML files first, then format each one individually. The formatter does not unpack ZIP archives.
Will it strip the BOM?
A leading UTF-8 Byte Order Mark is stripped during parsing. The output is plain UTF-8 without a BOM. This avoids the encoding pitfalls that BOMs cause in Unix shell scripts and on web servers.
Related tools
- YAML Formatter - clean up YAML config and manifest files.
- SQL Formatter & Beautifier - pretty-print SQL queries with standard keyword casing.
- Markdown Formatter - normalize headings, lists, and code blocks across Markdown files.
- XML Minifier - collapse formatted XML back into a single line for transport.
Related tools
ShotMark captures what you do here, in one click.
The traces, payloads, and tests you run by hand? ShotMark grabs the whole bug and hands it to your AI agent.