I wrote about [WayBack] XML Pretty Print in Online XML Pretty Print many years ago.
That’s all fine for small XML file and on-line usage.
A while ago however, I had two cases where off-line XML pretty printing turned out to be much easier than online XML pretty printing:
- pretty-print many XML files in one go
- compare large (100 megabyte plus) XML files
So I went searching, especially for a solution that would be available for both nx based platforms (Linux/Mac OS X/etc) and Windows which got me a few options in [WayBack] unix – How to pretty print XML from the command line? – Stack Overflow.
I found [WayBack] tidy
to be more practical than [WayBack] xmllint
as tidy
would split more lines which made it easier for Beyond Compare (that is very much cross platform!) to spot and show the differences.
Not just easier in the sense that the (already excellent) diff engine from Beyond Compare (which really sets it apart from other differencing tools) but very much easier on the eyes as now differences where on lines below each other in stead of 1000s of character positions in one line you had to scroll through.
There is two important things to remember with tidy
that both stem from its ancestry (it originates from an HTML
):
- you have to specify
-xml
- you have to specify the characterset (even though XML can specify it and without a definition, XML by default is
UTF8
,tidy
does not know about that)
These were the command-lines I used on Windows to do the pretty printing:
tidy -xml -utf8 QCScaper.test@borland.com.cds.xml > QCScaper.test@borland.com.tidy.cds.xml
xmllint --format QCScaper.test@borland.com.cds.xml > QCScaper.test@borland.com.pretty-printed.cds.xml
Another trick is to write a small Delphi program centered around the FormatXMLData
call which has been present since Delphi 2007 (see [WayBack] devnet.pdf), but documented since Delphi 2009 in [WayBack] XMLDoc.FormatXMLData Function.
Example code: [WayBack] delphi – How to reformat XML programmatically? – Stack Overflow.
An alternative is to use OmniXML: [WayBack] delphi – Nice bit of code to format an xml string – Stack Overflow
–jeroen
Related posts: