vek has asked for the wisdom of the Perl Monks concerning the following question:
The goal being to cleanup any empty block (in this example the MaintLevelInfo and MaintId blocks but there are more, sometimes 3 or 4 levels of empty blocks). The following code works but it just feels bad, I'm thinking there has to be a better way surely:<?xml version="1.0"?> <!DOCTYPE VersionMaint PUBLIC "- EN" "http://path-to/the-dtd"> <VersionMaint> <MaintLevelInfo> <MaintId> </MaintId> </MaintLevelInfo> <MaintRequest> <AppId>987624</AppId> <AppName>TriggerVar</AppName> </MaintRequest> </VersionMaint>
Needless to say there's nothing I can do about the way the XML is originally formatted, I'm just the lucky fellow who has to fix it.sub filterXML { my $xml = shift; my @xmlPieces = split (/\n/, $xml); my ($tmpFilter, $flag) = filter (\@xmlPieces); while ($flag) { ($tmpFilter, $flag) = filter ($tmpFilter); } my $filtered = join "\n", @$tmpFilter; return $filtered; } sub filter { my $items = shift; my (@new, $prev, $haveFiltered); for my $xmlLine (@$items) { my $test = $xmlLine; $test =~ s/^\s+//g; if ($test =~ /^<\// && $prev =~ /^<\w/) { unless ($prev =~ /<(.*)>(.*)<(.*)>/) { pop @new; $prev = $test; $haveFiltered++; next; } } push (@new, $xmlLine); $prev = $test; } return (\@new, $haveFiltered); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Cleaning Files
by theguvnor (Chaplain) on Jan 24, 2002 at 06:45 UTC | |
by vek (Prior) on Jan 24, 2002 at 10:25 UTC | |
by theguvnor (Chaplain) on Jan 24, 2002 at 22:15 UTC |