How to format XML on a Mac
You can do this with Note My: one command lays an XML document out, one collapses it. Here is exactly what each of them changes, what they leave alone, and how to corner a fault fast when a document arrives broken.
An XML document usually arrives in one of two unhelpful states. Either it is a single line four hundred kilobytes long, because something generated it and nothing was meant to read it, or it is laid out so generously that the structure you care about is three screens apart. Both are the same problem: the indentation does not match the nesting.
Note My has two commands for this. This guide covers what each one does to your document, how to run either on part of a file, and how to corner a fault fast when the XML arrives broken.
Both commands run on your Mac. There is no account and nothing is uploaded, so an XML document you format never leaves the machine.
Where the two commands live
They are in the Edit menu, alongside the JSON tools: Edit ▸ Format XML and Edit ▸ Minify XML. Two commands, one menu. They behave the same way on every document and on every Mac, so what you learn once holds.
The pair are opposites. One lays a document out so a person can read it; the other collapses it so something else can carry it. Both run on the whole document or on just the part you have selected, both are a single undo away, and both leave the file on disk exactly as it was until you save.
| Command | What you get | Reach for it when |
|---|---|---|
| Format XML | The document laid out so the indentation matches the nesting, with the whitespace inside your elements and your CDATA sections preserved | You have to read, review or edit the markup yourself |
| Minify XML | The same document collapsed back down, with empty elements written in their short form | Something downstream wants the document small — a request body, a fixture, a field with a length budget |
Everything below is the detail behind those two rows: what a format pass carries through untouched, how to aim either command at one branch, and how to corner a fault when a document arrives broken.
What Format XML changes, and what it leaves alone
Format XML lays the document out so the indentation matches the nesting. Two things about your content come through untouched, and one part of the markup is rewritten.
Whitespace is preserved
Whitespace inside an element is kept exactly as you had it. That matters more in XML than in JSON, because whitespace inside an element is content: the spaces in <name>Ada Lovelace</name> are part of the value, and in mixed content the spaces between inline elements carry meaning. Note My does not normalise them away, so a translation file comes back with its wording intact.
CDATA sections are preserved
CDATA is preserved too. A <![CDATA[ … ]]> block survives a format pass as a CDATA block, rather than being unwrapped and re-escaped into entities. If you are carrying embedded markup or a shell fragment inside one, that is the guarantee you want.
Empty elements are compacted
This is the one change to the markup rather than the layout. Both commands write an element with no children in its short form: <flag></flag> comes back as <flag/>. The two forms mean the same thing to anything reading XML. They are not the same to a diff, so expect that line to show as changed in version control.
Format an XML document
-
Open the file
Press ⌘O, or drag the file from the Finder onto the tab bar. If macOS insists the file is not text, use ⇧⌘O for File ▸ Open Anything as Text….
-
Decide on the scope
With no selection, the command works on the whole document. With text selected, it works on the selection only. To lay out the entire file, click once in the text so nothing is highlighted.
-
Run Edit ▸ Format XML
Choose the command and the document is laid out in one step — nothing to configure, nothing to confirm. If the markup is broken, nothing changes and you get a message.
-
Read the result, then save
Check the top of the file first: the declaration, the root element and the first level of nesting tell you whether you got what you wanted. ⌘Z reverses the whole transform as a single undo. When you are happy, ⌘S.
Formatting part of a document
Scope follows the selection, which is more useful than it first appears. A large document with one badly-generated branch does not need reflowing end to end; select that branch and format only it, and any hand-made alignment elsewhere survives. If you know the line the branch opens on, ⌘L is Go to Line — it comes up pre-filled with the current line, and a number past the end of the document is refused rather than clamped.
One habit makes this reliable: select from an opening tag through to its matching closing tag, including both, so the selection is a complete document in its own right. Anything else is reported back to you rather than half-transformed, so a slip of the mouse costs you nothing — adjust the selection and run the command again.
Minify XML, and when you actually want it
Edit ▸ Minify XML is the opposite command: it strips the layout out and hands the document back compact, with empty elements again in their short form. Reach for it when something downstream wants the document small — a payload you are pasting into a request body, a fixture you want on one line, a field with a length budget.
A minified document is aimed at the machine at the other end, so treat the result as a payload rather than as your working copy. Where whitespace inside an element carries meaning — a translated string, a paragraph of mixed content — read the collapsed version over before you commit to it. ⌘Z reverses the whole transform in one step, so the routine that suits this best is: save the readable version, minify, copy the compact text into wherever it is going, then undo and carry on with the file you were editing.
The two commands are happy to alternate. Lay a document out to work on it, collapse it when you need to hand it over, lay it out again the next morning. Neither command asks you to configure anything first, so switching between the two views of the same document is a menu choice rather than a task.
Corner a fault in broken markup
Neither command will touch a document it cannot lay out. If the markup is broken you get a message, not a half-transformed document, and nothing to undo.
When a pass reports a problem you are left with two useful things: a description of what went wrong, and a document still exactly as you left it. From there, cornering the spot is quick work. Three approaches find it fast, and you can combine them:
- Format progressively narrower selections. Format the first half of the document. If that succeeds, the fault is in the second half. Undo, and halve again. Because a failed attempt changes nothing, halving costs you nothing but a moment.
- Check for hidden characters. A zero-width character or a stray byte-order mark ahead of the declaration is invisible and fatal. Our hidden-character scanner flags what it turns up in the status bar, and Clean ▸ Clean Invisible / Special Characters… opens a review sheet that lists a Line:Col for every finding, which takes you straight to the character. A BOM at the very start of the file is left alone, because there it is legitimate; anywhere else it is flagged as junk.
- Turn on invisibles. The Show Invisibles toolbar toggle draws spaces, tabs and line endings without changing a byte, which often reveals a truncated write or a mixed line ending inside a tag.
Check that a document is well-formed
Use Format XML as the well-formedness check. It never changes a document it cannot lay out, so running it gives you either a formatted document or an error. Both answers are useful, and ⌘Z puts the layout back if the check was all you wanted.
It is worth running on anything that has been through a machine: a file a colleague exported, a response you pasted out of a request body, a fixture that has been hand-edited for a year. A moment in the Edit menu tells you whether the angle brackets add up before you pass the document on, and the layout you get for free makes the answer easy to read.
Two other tools pair well with it. Our comparison view puts a document next to a known-good one so a stray element shows up as a difference rather than as a hunch. And our find and replace, with its Regular Expression option, sweeps the whole document for a pattern — every element of one name, every attribute with an empty value — and will change them in one pass once you are sure.
Undoing a format or a minify
Neither command is one-way. ⌘Z reverses the whole transform in one step — not one element of it, so you never have to pick your way back through a document tag by tag.
Our auto-recovery safety net runs on its own and cannot be switched off, so a format you have not saved yet is not the only copy of your work. The unsaved marker is a leading * on the tab title.
If you want a route back that does not depend on undo at all, save before you run the command. File ▸ Reload from Disk (⌘R) then brings the saved version back and throws away everything since. On a document you care about, that is the safer habit: save first, then transform.
Troubleshooting
I get a message saying there is nothing to format
When there is nothing to work on, the command reports "There's no content to format…". The command works on your selection whenever there is one, so the usual cause is a stray selection — a drag you did not mean to make, or a few characters of whitespace left highlighted. Click once in the text to collapse it, which puts the whole document back in scope, and run the command again.
I get a message about the file not being UTF-8
You will see this when the text cannot be handled as UTF-8. Check the encoding pill in the status bar first. If the file was opened with the wrong encoding — accents replaced with rubbish — use File ▸ Encoding ▸ Reload with Encoding… to open it again correctly. If you instead want the file written as UTF-8 from now on, that is File ▸ Encoding ▸ Convert to Encoding…. The two are opposites: one changes how the file was opened, the other changes what will be saved. Our encoding handling covers both directions, so a document from an older system can be put right in the app.
I would like the markup coloured while I read it
You have the last word on how any tab is coloured. Click the Identify pill in the status bar to open the Language Inspector: it shows the language our advanced language identifier has chosen for the tab and gives you a pop-up to set it yourself, with Confirm or Cancel. HTML is built for angle-bracket markup and suits a document like this well: tags, attributes and values separate out at a glance, and typing > closes the tag for you. Our syntax engine applies the choice straight away — no need to quit and reopen — and the tab holds it while you work, so you can read the nesting in colour and run either command from the same Edit menu without losing your place.
The document is enormous
Neither command turns a document away for being too big. On a very large file, expect a short pause while the whole thing is laid out; give it a moment and it comes back complete. Our large-file engine keeps the editor itself comfortable on documents far bigger than most people ever open, so scrolling and typing stay smooth either way. And when only one branch has to be readable, select it and format that — scope follows the selection, so the size of the rest of the file stops mattering.
The document is read-only
Note My honours the lock on a read-only document rather than writing to it behind your back, so a locked export or a file opened straight off a disc image stays exactly as it is. Formatting is an edit like any other: save a copy somewhere you can write to, run the command there, and the layout comes out the same.
What else to do with the document once it is laid out
Formatting is usually the start of the job rather than the end of it. Once the nesting is visible, the rest of the app is pointed at the same document.
- Search it structurally. Our find and replace carries a Regular Expression option in the pop-over from the toolbar. On a laid-out document that is how you pull out every element of one name, every attribute with a given value, or every node that came back empty — and change them all in a single pass when you are ready.
- Search a whole tree of files. Search ▸ Find in Folder… (⇧⌘F) sweeps a directory of exports for a string or a pattern. It only ever reads the files it looks at; it never writes them, so pointing it at a folder you care about is safe.
- Compare two versions. Our comparison view sets one document beside another, so a single changed attribute between yesterday's export and today's shows up as a difference rather than as a suspicion.
- Clean what you cannot see. Our hidden-character scanner and Clean ▸ Clean Invisible / Special Characters… catch the zero-width characters and stray marks that ride along with copied markup, and show you each one before anything is removed.
- Repeat a tidy-up. Our macro recorder captures a sequence of editing steps you worked out once, so the next document of the same shape is one pass instead of ten.
- Start from something known-good. Keep a skeleton document in our template library and open a fresh copy when you need one, rather than cutting an old export back down to its bones.
Frequently asked questions
Where do I find the two XML commands?
Both are in the Edit menu, alongside the JSON tools: Edit ▸ Format XML and Edit ▸ Minify XML. Choose one and it runs straight away — on your selection when you have one, and on the whole document when you do not.
Will formatting change my data?
The layout changes, and empty elements are written in their short form. Whitespace inside your elements and CDATA sections are preserved. If the markup is broken, nothing is changed at all.
How do I find the part of the document that is broken?
Halve it. Format the first half of the document; if that half comes out laid out, the fault is in the second. Undo, halve again, and a handful of passes puts you on the element. Every attempt that reports a problem leaves your document exactly as it was, so each round costs you nothing but a moment. Our hidden-character scanner does the other half of the job, turning up the zero-width characters and stray marks that break a document invisibly.
Can I format only one element instead of the whole file?
Yes. Select it and run the command; scope is the selection when there is one. The selection has to be a document in its own right, so select from an opening tag through its matching closing tag.
Can I put a laid-out document back on one line?
Yes. Edit ▸ Minify XML is the return trip: it collapses the layout and hands the document back compact, ready for a request body or a field with a length budget. It honours a selection in the same way, and ⌘Z reverses the whole transform in one step — so you can lay a document out to work on it, collapse it to hand it over, and lay it out again the next morning.
Where does the formatting happen?
It all happens on your Mac. There is no account, no sign-in and no telemetry, and your files never leave the machine, so a payload with real customer records in it can be laid out where it already sits.