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…)

Categories
Uncategorized

13 Years of RasterWeb!

13 Years

13 years of blogging… You can browse through the archives if you like. It’s all there.

I just keep going. I don’t know why… probably because it’s hard to stop. I don’t give up easily, and I like to keep trying. Things were obviously very different in 1997 when we started. There were a handful of blogs and you could read them all during your lunch hour. As much as I’m saddened to see an old friend abandon their blogs, I’m often quite pleased to see some are still at it.

This is the part where I thank Dave Winer, because without him, I wouldn’t have started blogging. (Even if he does block me on Twitter.)

I’m considering a few new taglines, perhaps: “Wisconsin’s longest running blog” or something… I dunno, any suggestions?

Anyway, I’m just here to congratulate myself on 13 years of blogging, and hope for another 13 to come… Keep up the good work, self!