To format XML in IntelliJ IDEA, open the file and press Ctrl+Alt+L on Windows or Linux, or Cmd+Option+L on macOS, to trigger the Reformat Code action, which applies your configured Code Style → XML settings for indentation, line breaks, and attribute wrapping. If the IDE doesn't recognize the file as XML and the bottom-right status bar shows "Plain Text", assign the XML file type from the status bar menu or via Settings → Editor → File Types before reformatting, otherwise the action treats the content as plain text and skips indentation rules. For moments when the IDE is not available, when the XML arrived as one long minified line, or when you simply want a clean pretty-printed view without opening a project, a browser-based XML Formatter takes the raw text, lets you pick 2-space, 4-space, or tab indentation, and reformats or minifies the result without uploading anything to a server. Both approaches share the same goal: turning a hard-to-read XML blob into a layout that mirrors its actual nesting depth so every element sits at the level it really occupies.

how to format xml in intellij
Format XML in IntelliJ IDEA: Shortcuts and Browser Option

IntelliJ IDEA's Built-in XML Formatting Tools

IntelliJ IDEA bundles a complete XML-aware editor that supports formatting, structure validation, namespace imports, code-style configuration, and code-structure navigation out of the box. JetBrains documents XML as a first-class language in the IDE, with shortcuts for reformatting (Ctrl+Alt+L / Cmd+Option+L), indentation adjustments (Ctrl+Alt+I / Ctrl+Alt+I), and a dedicated Code Style → XML page where every aspect of the formatted output is controllable. The Reformat Code action walks through the document and applies your configured indent size, attribute wrapping rules, blank-line conventions, and tag alignment, and it works on the entire file by default or on a highlighted selection if you select a region first. Because the IDE parses the XML before formatting, it understands where tags actually start and end, which avoids the kind of broken output that regex-based pretty-printers often produce on attribute values containing > or on self-closing elements.

Format XML in IntelliJ IDEA: Step by Step

  1. Open the XML file in the editor. If the status bar at the bottom-right says "Plain Text" instead of "XML", click it and choose XML so the IDE applies XML-aware parsing, completion, and formatting rules.
  2. Place the cursor anywhere in the file, or select a specific region you want to format. Selection-based formatting applies only to the highlighted block.
  3. Press Ctrl+Alt+L on Windows or Linux, or Cmd+Option+L on macOS. This is the Reformat Code shortcut and it works across most JetBrains IDEs including WebStorm, PyCharm, GoLand, RubyMine, and Android Studio.
  4. If a dialog appears asking whether to format the whole file or just the selection, pick the option that matches your intent. Most users pick "Whole file" the first time and switch to selection mode afterward.
  5. Review the changes in the editor. IntelliJ shows a diff indicator in the gutter when the action touches more lines than expected, so you can quickly spot anything you want to undo.
  6. Save with Ctrl+S on Windows or Linux, or Cmd+S on macOS, to commit the formatted version to disk.

For an alternative that preserves your existing line breaks and only normalizes leading whitespace, use Auto-Indent Lines (Ctrl+Alt+I on Windows/Linux, Ctrl+Alt+I on macOS in most JetBrains IDEs). This is useful when a colleague hand-edited the file and you only want the indentation corrected, not the entire layout rebuilt.

Configure XML Code Style in IntelliJ Settings

Reformat Code uses whatever rules live in Settings → Editor → Code Style → XML on Windows or Linux, or IntelliJ IDEA → Preferences → Editor → Code Style → XML on macOS. Changes here affect every Reformat Code pass until you change them again, and a preview pane on the right side of the dialog shows how the formatted output will look before you apply anything. Inside the dialog the controls are split across tabs that mirror the official JetBrains documentation:

  • Tab and Indent: switch between "Use tab character" and spaces, set the indent size (2 or 4 are common), set continuation indent for wrapped attribute lists, and decide whether to keep existing indents when reformatting.
  • Smart tabs: enable mixed tab-and-space indentation, where leading whitespace uses tabs and alignment padding uses spaces, for projects that follow that convention.
  • Wrapping and Braces: configure right-margin width, hard wrap at margin, wrap on typing, visual guides, and the Keep when reformatting section to pin attributes or text blocks to their original formatting even when the surrounding code is reformatted.
  • Blank Lines: set minimum blank lines around top-level elements, inside tags, and before or after specific tag groups, which is useful for separating function-like blocks in configuration files.

Once configured, you can apply the style across an entire project using Code → Reformat Code, or even from the command line through the JetBrains formatter tool, which formats files according to the same project-level style without launching the full IDE. For an IntelliJ-centric JSON workflow alongside this one, the same shortcut pattern applies and is broken down in Format JSON in IntelliJ IDEA: Shortcuts and Browser.

When a Browser-Based XML Formatter Helps

IntelliJ is the right tool when you are already inside the project, when the file type is recognized, and when your code style is consistent with the rest of the codebase. There are moments, though, when the IDE gets in the way more than it helps:

  • The XML came from a server response or a log snippet and is not part of any open project.
  • Someone sent you a single-line minified payload and you just want to read what it actually contains.
  • You are reviewing code on a machine where the IDE is not installed or licensed.
  • You need to format XML that is mixed with other content and you don't want IntelliJ to reformat the wrong file by accident.
  • You are working with sensitive XML, such as internal API responses, proprietary configuration, or customer data, and you want the bytes to stay on your device.

For these situations, a focused browser tool handles the job without the IDE's overhead.

Format XML in Your Browser with the XML Formatter

  1. Paste or type your XML into the input box on the XML Formatter page.
  2. Pick an indentation style from 2 spaces, 4 spaces, or Tab, depending on your project's convention.
  3. Click Format to pretty-print the document with consistent indentation, or click Minify to compress it onto a single compact line.
  4. Review the result. If the input has malformed XML, the tool surfaces a friendly error message, usually with a line and column reference, and you can fix the source before re-running.
  5. Click Copy to copy the formatted or minified output to your clipboard, then paste it wherever you need.

Everything happens locally in your browser using JavaScript text processing, so your XML is never uploaded, never sent to a server, never logged, and never stored. That makes the tool safe for sensitive configuration, internal API responses, or proprietary data files, and it keeps working even with the network disconnected.

Format vs Minify: Which Direction Do You Need

Format and Minify serve opposite purposes. Format expands whitespace and rebuilds indentation so the structure is easy to scan; Minify strips insignificant whitespace and produces a single compact line to shrink payload size without changing meaning. The table below matches common scenarios to the direction that fits them.

Use casePreferred directionWhy it fits
Reading an unfamiliar XML payloadFormatIndentation reveals nesting at a glance
Debugging mismatched or stray tagsFormatVisual structure makes the imbalance obvious
Sending XML over a networkMinifyRemoves whitespace without changing meaning
Embedding XML inline in source codeMinifyOne-line form fits inline without line breaks
Code review of a hand-edited fileFormatSurfaces inconsistent indentation
Storing XML in a size-constrained cacheMinifySmallest payload that still parses the same way

What the Browser-Based XML Formatter Preserves Correctly

Naive find-and-replace pretty-printers break XML in subtle ways. The XML Formatter is built around XML's tag-and-attribute model and handles the constructs that trip up simpler tools. Attribute values that contain a greater-than character parse correctly instead of cutting the tag short. Self-closing elements stay exactly as written, so <br/> remains <br/> rather than being expanded. Comments stay in place and indent with the rest of the document, so author notes are not lost. CDATA sections pass through untouched, so the literal characters inside them, including reserved symbols and markup-looking text, survive both formatting and minifying. The XML declaration and processing instructions stay on their own line at the top, and DOCTYPE declarations are preserved verbatim.

When an element mixes text with inline child elements, the kind of markup you see in SVG text, XHTML, or documentation formats, that whole run is kept on a single line exactly as written, so no meaningful whitespace inside it is rewritten; formatting only re-indents the space that sits between tags. Unlike a JSON formatter, which cannot process XML at all because JSON has no attributes, comments, CDATA, namespaces, or processing instructions, this tool is built specifically for XML's structure and special constructs. While reformatting, the parser flags structural problems it encounters, such as unclosed tags, mismatched or stray closing tags, and unterminated comments, CDATA sections, or processing instructions, with a friendly message and, where possible, a line and column reference. It is not a full schema or DTD validator, so it does not check your document against an XSD, verify data types, or enforce namespace rules; think of it as a formatter that refuses to silently emit broken output, not as a compliance checker.

For a deeper look, see Convert XML to CSV File Without Uploading It.