xml parsing in perl

Although you’re strongly advised against writing your own XML parser, the standard ones can’t cope well with badly formatted XML input. The stuff I was trying to use wasn’t well formed, so I had to write something that didn’t care. It’s only four lines, I can’t see what all the fuss is about. Encodings blah.

# Chris's recursive xml parser that doesn't handle quotes properly

my $d=0;

while () {

sub p {
my ($t,$a,$c) = $_ =~ m::;
if ($t =~ m:^/:) { print "t" x $d,"+++$t+++ nnn"; $d--; return; } else { print "t" x $d,"+++$t+++n"; }
if (defined($a)) { foreach my $kv (split(/ /, $a)) { if (my ($k, $v) = $kv =~ m/^(w+)="(.*)"/) { print "t" x $d,"$k = $vn"; } } }
if (defined($c)) { print "nn"; } else { print "nn"; $d++; while () { p($_); } }

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment replies are not available offline