I downloaded several pre-made configuration profiles (xml) but when I use Xcode to edit each of them, the indentation formatting for each keys, string, integer, elements, etc came in one single line. I am talking about the entire document. Example:
<dict><key>OfficeAutoSignIn</key><true/><key>ShowWhatsNewOnLaunch</key><false/><key>DefaultsToLocalOpenSave</key><true/></dict></dict></array></dict></dict></dict><dict><key>PayloadUUID</key><string>1A314508-09FV-40C6-80F4-B98AAQWEC8D</string><key>PayloadType</key><string>com.apple.ManagedClient.preferences</string><key>PayloadOrganization</key><string>XYZ</string><key>PayloadIdentifier</key><string>CD2345508-40F3-40C6-80F4-TY7WSDDAC8D</string><key>PayloadDisplayName</key><string>Custom</string><key>PayloadDescription</key><string/><key>PayloadVersion</key><integer>1</integer><key>PayloadEnabled</key><true/><key>PayloadContent</key><dict><key>com.microsoft.Office365ServiceV2</key><dict><key>Forced</key><array><dict><key>mcx_preference_settings</key><dict><key>SendAllTelemetryEnabled</key><true/></dict></dict></array></dict></dict></dict><dict><key>PayloadUUID</key><string>NMJK78543-42EE-41F7-97D7-765CC1832E08</string><key>PayloadType</key><string>com.apple.ManagedClien....etc...etc..
Of course, I can place the mouse cursor at the end of each element and press return and Xcode will automatically fix the indentation in the correct way, but it will take me for ever to do this for each files.
I was wondering, in Xcode, by doing Select All (select the entire code of the xml file), there is any option that magically will put the entire code in the correct indentation format with all the correct tab spaces?
2
Answers
xmllint --format input.xml > formatted.xml
Xcode does not have a prettify/format command for XML, but there are a few options:
You can format it from the command line:
From Xcode, you could also choose to view the XML as a plist:
That will yield:
Then manually save it (e.g. command+s or “File” » “Save”). If you switch back to viewing it as “Source Code”, you will see:
Like the prior technique, this adds a little cruft of the file (the
<?xml>
and, in this case, the<!DOCTYPE>
and<plist>
and</plist>
tags, too), which you may have to delete, but perhaps this is easier than manually adding all the newline characters yourself.Another approach is to insert newline before every new tag and then re-indent. So, use Xcode find-and-replace, “Find” » “Find and Replace…” or option+command+f, searching for
<
and replacing with⏎<
.Note, to enter the
⏎
newline character in the Xcode “With” box, press control+option+command+p. When you are done entering the “Replace” field of<
and the “With” field with⏎<
, you can tap on the “All” button to do all the replacements.Obviously, now that you have everything on its own line, you can format it with command+a (to select all) followed by control+i (to re-indent).
If you have access to other editors, see if you can reformat your code there. E.g. DreamWeaver’s “Edit” » “Code” » “Apply Source Formatting” reliably formats the code very nicely, without adding any cruft. It is overkill for such a simple task, but it does it well.