skip to Main Content

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


  1. Chosen as BEST ANSWER

    xmllint --format input.xml > formatted.xml


  2. Xcode does not have a prettify/format command for XML, but there are a few options:

    1. You can format it from the command line:

      xmllint --format -o output.xml input.xml
      
    2. From Xcode, you could also choose to view the XML as a plist:

      enter image description here

      That will yield:

      enter image description here

      Then manually save it (e.g. command+s or “File” » “Save”). If you switch back to viewing it as “Source Code”, you will see:

      enter image description here

      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.

    3. 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 ⏎<.

      enter image description here

      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).

    4. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search