idarkstranger (idarkstranger) wrote in macosxdev,

NSXMLDocument how i can format ?

i have some XML document and i want formatted his

sample
this source xml !
<first><second><third></third></second></first>

i want save source xml and see in my xml file )))

<first>
    <second>
          <third>
          </third>
   </second>
</first>
  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

  • 1 comment

drmellow

January 10 2011, 13:15:56 UTC 3 years ago

This will get you close to what you're looking for. In Terminal, use the command xmllint with the --format option. When I tried, it added an xml declaration to the top of the code (which you can later delete if you really don't want to keep it, but you probably want it anyway). It also collapsed the third element to be self-closing instead of having a an opening and closing tag.

E.g., here's what it looks like to me:

$ cat tmp.xml 
<first><second><third></third></second></first>


$ xmllint --format tmp.xml 
<?xml version="1.0"?>
<first>
  <second>
    <third/>
  </second>
</first>

That just displays the output. If you want to save it, you need to redirect the output to a new file:

$ xmllint --format tmp.xml > new.xml

There are also probably text editors, etc. that have options to reformat XML in place, but this is an easy way to use the command-line to do it.