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.

In reply to Comparing two XML Files with XML::Twig by DannMan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.