Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^5: Compare 2 XML files

by Corion (Patriarch)
on Jul 10, 2017 at 11:55 UTC ( [id://1194679]=note: print w/replies, xml ) Need Help??


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

Now is the moment when you refer to perlfaq4 to get the "intersection of two arrays", as I already told you.

Maybe we are talking past each other here. This is not a code writing service. You are expected to write the code yourself. I will help you by pointing you to the resources you need to write your code for your work, but you will have to do the work yourself.

Replies are listed 'Best First'.
Re^6: Compare 2 XML files
by snehit.ar (Beadle) on Jul 10, 2017 at 13:44 UTC
    I have gone through the perlfaq4 ,but I am really struggling to get the correct logic to compare two array reference and only display matching records... Its also says use of Hash, so do i need to change Hash ...as im already having array of hash.. please correct me ..
    #!/usr/bin/env perl use strict; use warnings; use XML::XPath; use Data::Dumper; my @records; my @eventrecords; my $eventxml = 'events.xml'; my $evenxp = XML::XPath->new(filename => $eventxml); my $evennodeset = $evenxp->findnodes('//event'); my $xml = 'ApplicationList.xml'; my $xp = XML::XPath->new(filename => $xml); my $nodeset = $xp->findnodes('//application_list'); foreach my $evennode ($evennodeset->get_nodelist) { my $evenssrid = $evenxp->find("./custom_attribute_list/custom_ +attribute[normalize-space(name)='SSRID']/value", $evennode); s/^\s+|\s+$//g for $evenssrid; push @eventrecords, {eventid => $evenssrid}; } print Dumper \@eventrecords; foreach ($xp->findnodes('/application_list/application/@id') ) { my $appid = $_->string_value; $appid =~ s/^\s+|\s+$//g; push @records, {appid => $appid }; } print Dumper \@records;
      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
        Brilliant @Poj - Thank you for helping ...

      You have the "application" XML (the left side) and the event XML (the right side) in your comparison.

      You will need a hash to know whether an element is in the application XML array.

      For that, you initialize the hash using the application id as key and (say) the hash reference from the right side as the value.

      Then, for each event in the event array, you look in the application ID hash for the application id. If it exists, you have found a match.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1194679]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-24 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found