[Israel.pm] Is XML beautifying more difficult than I think?

Yuval Yaari yuval at windax.com
Tue May 11 03:21:58 PDT 2004


Hi,

A co-worker asked me if I know of a Perl module for XML beautifying.
XML::Beautify still can't handle strings, so I didn't really manage to 
find something...

I wrote this in less than 5 minutes (copy-pasted from emacs so it may or 
may not look good in your mail client):

## BEGIN CODE ##

#!/usr/bin/perl -w
use strict;
use diagnostics;

open(XML, "file.xml");
my @Data = <XML>;
close(XML);

chomp(@Data);

my $indent_level = 0;
my @last_tags = ();

foreach my $line (@Data) {
        $line =~ s/^\s+//;
        next if (length($line) == 0);
        if ($line =~ m!<(\w+)>!) {
                print "\t" x $indent_level;
                unless ($line =~ m!</$1>!) {
                        $indent_level++;
                        push(@last_tags, $1);
                }
        } elsif ($line =~ m!</$last_tags[-1]>!) {
                $indent_level--;
                pop(@last_tags);
                print "\t" x $indent_level;
        } else {
                print "\t" x $indent_level;
        }
        print $line . "\n";
}
__END__

Ok, it may be ugly, but it works flawlessly on the files we needed to 
"beautify".
I don't really like XML so I'm far from being an XML guru - are there 
any gotchas that I'm missing?
Otherwise, I'd refactor it a bit and throw comments and perhaps someone 
will make use from this piece of code.


  --Yuval




More information about the Perl mailing list