Prettify (indent) XML files
Problem
You have an XML file but it’s not indented. Maybe the whole contents is in one line… How to transform it to a human readable form?
Solution
xmllint --format ugly.xml
Example:
Input:
<?xml version="1.0" encoding="UTF-8"?> <gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref"> <gesmes:subject>Reference rates</gesmes:subject> <gesmes:Sender> <gesmes:name>European Central Bank</gesmes:name> </gesmes:Sender> <Cube> <Cube time='2011-01-10'> <Cube currency='USD' rate='1.2903'/> <Cube currency='JPY' rate='107.17'/> </Cube> </Cube> </gesmes:Envelope>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time="2011-01-10">
<Cube currency="USD" rate="1.2903"/>
<Cube currency="JPY" rate="107.17"/>
</Cube>
</Cube>
</gesmes:Envelope>
Related Work
If you want to edit your XML files, you can try the oXygen XML editor. With oXygen you can do anything that you can imagine… If you register, you get a code and you can use the software for 30 days. Registration can be renewed.
Do you know how to save this output to the same file??
For example if I do this:
The output of the contents it’s saved in that file, but if I do this:
I got:
Is there a way to overwrite the same file?? Thanks for this article thought ;)
It’s not a good idea to redirect the output of xmllint to the input file. This way you can corrupt the input file, even lose its content. I suggest using a temp file: