Categories
Uncategorized

Pretty Print XML with Perl

Let’s say you’ve got a file named “file.xml” and want it pretty printed, all indented nice and everything…

For just such an occasion I have a Perl script named “pretty.pl” and I just run my XML file through it like so: cat file.xml | perl pretty.pl

Here’s the code I use:

#!/usr/bin/perl

use XML::Twig;
use XML::Parser;

my $xml = XML::Twig->new(pretty_print => 'indented');

$xml->parse(\*STDIN);

$xml->print();

You can even pass it through right as it comes in over the wire: curl http://example.com/data/file.xml | perl pretty.pl

Here’s an example of data from Foursquare without pretty printing. (I used curl to grab the data. Also, I added in some line breaks, just to make it a little more readable.):

<?xml version="1.0" encoding="UTF-8"?>
<checkins><checkin><id>123847273</id>
<created>Mon, 09 Aug 10 00:50:33 +0000</created>
<timezone>America/Chicago</timezone><venue><id>2357761</id>
<name>The Kiltie</name><primarycategory><id>79067</id>
<fullpathname>Food:Ice Cream</fullpathname><nodename>Ice Cream</nodename>
<iconurl>http://foursquare.com/img/categories/food/icecream.png</iconurl>
</primarycategory><address></address><city></city><state></state>
<geolat>43.107391</geolat><geolong>-88.464475</geolong></venue>
<display>Pete P. @ The Kiltie</display></checkin></checkins>

And here’s the same data, again using curl to grab it, and then passing it through the pretty.pl script:

<?xml version="1.0" encoding="UTF-8"?>
<checkins>
  <checkin>
    <id>123847273</id>
    <created>Mon, 09 Aug 10 00:50:33 +0000</created>
    <timezone>America/Chicago</timezone>
    <venue>
      <id>2357761</id>
      <name>The Kiltie</name>
      <primarycategory>
        <id>79067</id>
        <fullpathname>Food:Ice Cream</fullpathname>
        <nodename>Ice Cream</nodename>
        <iconurl>http://foursquare.com/img/categories/food/icecream.png</iconurl>
      </primarycategory>
      <address></address>
      <city></city>
      <state></state>
      <geolat>43.107391</geolat>
      <geolong>-88.464475</geolong>
    </venue>
    <display>Pete P. @ The Kiltie</display>
  </checkin>
</checkins>

I still find Perl extremely useful for this sort of task… I’m sure there are other command line ways to do this, but this one works for me.

(Hat tip to A Curious Programmer where I picked up this Perl code from…)