DannMan has asked for the wisdom of the Perl Monks concerning the following question:

I have two XML files, (provided by another application) that needs a comparison. Some of the parameters (especially in the ifIndex section) does change from time to time. Here is a snippet from one of the files..
<tmsinfo> <rtr name="router1"> <ifIndex value="127"> <ifDescr value="FastEthernet0/0/1.87"/> <ifSpeed value="100000000"/> <ipAdEntIfIndex value="10.0.0.1"/> </ifIndex> <ifIndex value="32"> <ifDescr value="Serial0/1/1:0"/> <ifSpeed value="256000"/> <ipAdEntIfIndex value="10.0.0.2"/> <locIfDescr value="SomeLink"/> </ifIndex> . .cut . <ifIndex value="146"> <ifDescr value="FastEthernet0/0/1.95"/> <ifSpeed value="64000"/> <ccarConfig ccarDir="1"> <ccarIndex value="1"> <AclNo value="2160"/> <ExtLimit value="8000"/> <Limit value="8000"/> <Rate value="64000"/> </ccarIndex> </ccarConfig> <ccarConfig ccarDir="2"> <ccarIndex value="1"> <AclNo value="3160"/> <ExtLimit value="8000"/> <Limit value="8000"/> <Rate value="64000"/> </ccarIndex> </ccarConfig> <ipAdEntIfIndex value="10.0.0.3"/> </ifIndex> </rtr> </tmsinfo>
What I have tried so far is to load one file into a twig and use the TwigHandlers on the second file to do the comparison on if the information inside the ifIndex has changed.
#!/usr/bin/perl -w use strict; use XML::Twig; my $router=$ARGV[0]; my $fname="$router.xml"; my $RoutersLastState = XML::Twig->new(); #Last received State ### Load Last State/s $fname="$fname.last"; $RoutersLastState->parsefile($fname); print "Loaded Last State\n"; my $RoutersCurrState = XML::Twig->new( TwigHandlers => { ifIndex => \ +&find_diff }, ); #Current State ### Load Current State $RoutersCurrState->parsefile($fname); print "Loaded Current\n"; my $outfname="$fname.changes"; open (FH,"> $outfname"); $RoutersCurrState->print(\*FH,'indented'); close (FH); exit; sub find_diff{ my( $twig, $ifindex)= @_; my $indexval=$ifindex->att('value'); # print "$indexval\n"; foreach my $oldindex ($RoutersLastState->root->first_child('router') +->children('ifIndex')){ my $oldindexval=$oldindex->att('value'); print "$indexval\t$oldindexval \n"; if ($indexval eq $oldindexval){ $ifindex->cut; print "found one \n"; } } }
I deleted a whole ifIndex section from one of the files, but still end up with an "empty" result. Thanks in advance for any help.

Replies are listed 'Best First'.
Re: Comparing two XML Files with XML::Twig
by pzbagel (Chaplain) on May 29, 2003 at 00:03 UTC

    Your code is basically correct except that you change the $fname variable by adding the .last extension before it is used to open the a file. So this same value($router.xml.last) is used for both XML::Twig objects, hence you get files that are identical. Try replacing the following:

    $fname="$fname.last"; $RoutersLastState->parsefile($fname); print "Loaded Last State\n";

    with this:

    $RoutersLastState->parsefile("$fname.last"); print "Loaded Last State\n";

    Your results will now end up in $router.xml.changes instead of $router.xml.last.changes.

    HTH

Re: Comparing two XML Files with XML::Twig
by DannMan (Initiate) on May 28, 2003 at 23:59 UTC
    Sorry monks.. missed a obvious fault. the $fname variable was declared wrongly.. Thanks anyway