Hello
JSON or XML? It depends .... XML has, for me, preference in formal communication with external parties because you can document it more verbose and use the xsd for proofing that the messages are according agreements between parties. JSON is more dense and easier for a computer to transform from/to objects, and is more used as a carrying mechanism between server and client (think javascript in browsers that query the back-end.)
For the problem at hand, find below a quick and dirty solution, using XML::Simple, Time:Piece (core module) and some sorting and mapping according the Camel handbook. I can really recommend this book.
Not tested in full and I didn't look for performance! Also be aware for datetime conversions. Check if they are appropriate for your situation. The Data::Dumper is there for testing purposes.
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
use Time::Piece;
my $xml = XMLin( 'depart.xml',
ForceArray => ['flightStatus'], # To force the flightstatu
+ses into an arrayref
GroupTags => { # To eliminate unnecessary leve
+ls in the structure
airlines => 'airline',
airports => 'airport',
equipments => 'equipment',
flightStatuses => 'flightStatus'});
# just to illustrate that you can easily extract the data
my $airports = $xml->{appendix}{airports};
my $equipments = $xml->{appendix}{equipments};
my $airlines = $xml->{appendix}{airlines};
my @flightstatuses = @{$xml->{flightStatuses}}; # make it a normal arr
+ay from flightstatuses
print "Airports\n";
print "--------\n";
print Dumper $airports;
print "Airlines\n";
print "--------\n";
print Dumper $airlines;
print "Equipments\n";
print "----------\n";
print Dumper $equipments;
print "Flightstatuses\n";
print "----------\n";
print Dumper (\@flightstatuses);
# examples of obtaining an element of a flightstatus that i use in the
+ sorting
# take the first element
my $flightstatus = $flightstatuses[0];
# obtain departure and arrival times
my $departuretime = $flightstatus->{departureDate}{dateUtc};
my $arrivaltime = $flightstatus->{arrivalDate}{dateUtc};
print $departuretime, "\n";
print $arrivaltime, "\n";
# printed after conversion (remark: didn't take the '000Z' in my conve
+rsion)
print Time::Piece->strptime($departuretime, '%Y-%m-%dT%H:%M:%S.000Z'),
+ "\n";
print Time::Piece->strptime($arrivaltime, '%Y-%m-%dT%H:%M:%S.000Z'), "
+\n";
# transform the times into epoch, needed for sort function (strictly,
+it is not, because a Time::Piece
# object will stringify to epoch
my $departuretime_epoch = Time::Piece->strptime($departuretime, '%Y-%m
+-%dT%H:%M:%S.000Z')->epoch;
my $arrivaltime_epoch = Time::Piece->strptime($arrivaltime, '%Y-%m-%dT
+%H:%M:%S.000Z')->epoch;
print $departuretime_epoch , "\n";
print $arrivaltime_epoch, "\n";
# the classic example of the camel cookbook to sort with mapping where
+ the mappings shown in the examples are used.
my @sorted_by_arrival = map {$_->[1]}
+# map back to original array
sort {$a->[0] cmp $b->[0]} # cla
+ssical compare of two values
map {[Time::Piece->strptime($_->{arrivalDate}{dateUtc}
+, '%Y-%m-%dT%H:%M:%S.000Z')->epoch, $_]} # make a intermediate array
+with on first place the value used to sort
@flightstatuses;
my @sorted_by_departure = map {$_->[1]}
+ # map back to original array
sort {$a->[0] cmp $b->[0]} # c
+lassical compare of two values
map {[Time::Piece->strptime($_->{departureDate}{date
+Utc}, '%Y-%m-%dT%H:%M:%S.000Z')->epoch, $_]} # make a intermediate ar
+ray with on first place the value used to sort
@flightstatuses;
# mapping to be able to print the results
my @sorted_by_arrival_id = map {$_->{'flightId'}} @sorted_by_arrival;
my @sorted_by_departure_id = map {$_->{'flightId'}} @sorted_by_departu
+re;
print join " ", @sorted_by_arrival_id, "\n";
print join " ", @sorted_by_departure_id, "\n";
Hope this will helps
Martell