in reply to Re^6: Compare 2 XML files
in thread Compare 2 XML files

so do i need to change Hash ?

Yes, and parse the application ids first

#!/usr/bin/env perl use strict; use warnings; use XML::XPath; use Data::Dumper; my $xml = 'ApplicationList.xml'; my $xp = XML::XPath->new(filename => $xml); my %appid = (); for ($xp->findnodes('/application_list/application/@id') ) { my $id = $_->string_value; $id =~ s/^\s+|\s+$//g; $appid{$id} = 1;; } print Dumper \%appid; my $eventxml = 'events.xml'; my $evenxp = XML::XPath->new(filename => $eventxml); my $xpath = "//event/custom_attribute_list/custom_attribute[normalize- +space(name)='SLB_SSRID']/value"; my @eventrecords = (); foreach my $node ($evenxp->findnodes($xpath)) { my $ssrid = $node->string_value; $ssrid =~ s/^\s+|\s+$//g ; if ( exists $appid{$ssrid} ){ push @eventrecords, { eventid => $ssrid }; } } print Dumper \@eventrecords;
poj

Replies are listed 'Best First'.
Re^8: Compare 2 XML files
by snehit.ar (Beadle) on Jul 11, 2017 at 05:29 UTC
    Brilliant @Poj - Thank you for helping ...
      Hello Poj, i have modify the code as below :
      my $xml = 'ApplicationList.xml'; my $xp = XML::XPath->new(filename => $xml); my $appxpath = $xp->findnodes("//application_list/application/"); my %appid = (); my %appname = (); foreach my $appnodeset ($appxpath->get_nodelist) { my $id = $xp->find('./@id',$appnodeset); my $name = $xp->find('./@name',$appnodeset); #$name = $_->string_value; s/^\s+|\s+$//g for $id,$name; $appid{$id} = {$name->string_value}; } print Dumper \%appid;
      But i am getting output as
      $VAR1 = { '1103' => { 'cccc' => undef }, '2667' => { 'bbbb' => undef }, '957' => { 'aaaa' => undef }, '1503' => { 'dddd' => undef } };
      But i want out put as
      $VAR1 = { '1103' => 'cccc', '2667' => 'bbbb', '957' => 'aaaa', '1503' => 'dddd' };

        $appid{$id} = $name->string_value; So what do you think putting braces around then contents did?
        What do braces mean?
        What happens when there isnt an even number of values inside braces?

        You need to understand the answers to those questions or you will just keep getting into trouble.

        Get string values before regex

        #!/usr/bin/env perl use strict; use warnings; use XML::XPath; use Data::Dumper; my $xml = 'ApplicationList.xml'; my $xp = XML::XPath->new(filename => $xml); my $appxpath = $xp->findnodes("//application_list/application"); # no + end / my %appid = (); foreach my $appnodeset ($appxpath->get_nodelist) { my $id = $xp->find('./@id',$appnodeset)->string_value; my $name = $xp->find('./@name',$appnodeset)->string_value; s/^\s+|\s+$//g for $id,$name; $appid{$id} = $name; } print Dumper \%appid;