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

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


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


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

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


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>!) {
                        push(@last_tags, $1);
        } elsif ($line =~ m!</$last_tags[-1]>!) {
                print "\t" x $indent_level;
        } else {
                print "\t" x $indent_level;
        print $line . "\n";

Ok, it may be ugly, but it works flawlessly on the files we needed to 
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.


More information about the Perl mailing list