Search code examples
xmllinuxbashubuntuxml-formatting

XML formatting Indentation Tags Matching - Linux


I have an XML file whose format is quite compressed and all tags are stick together like

<PersonalData><IndividualDetails><Title>Mr</Title><Gender>Male</Gender><FirstName>Hae</FirstName><Surname>JONES</Surname><Occupation>Banker</Occupation><DateofBirth>4/6/76</DateofBirth><LastKnownAddress></LastKnownAddress><LastKnownPostCode>00145</LastKnownPostCode><OtherNames></OtherNames></IndividualDetails><OccupationDetails><Company>SD Bank</Company><CompanyAddress>Sunset Boulevard NY</CompanyAddress><ContactNo>335698457</ContactNo></OccupationDetails></PersonalData>

Is there any command in shell that can properly format the tags. If not indentation only adding the tags to their own lines can also solve my problem.


Solution

  • xmllint --format <your-xml-file>
    

    example

    $ cat test.xml
    <a><b>c</b></a>
    $ xmllint --format test.xml
    <a>
      <b>c</b>
    </a>
    $ xmllint --format test.xml > test.formatted.xml
    $ cat test.formatted.xml
    <a>
      <b>c</b>
    </a>
    $