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

Re: Looking for suggestions, sorting data from XML or JSON

by martell (Hermit)
on Jul 06, 2013 at 12:36 UTC ( [id://1042904]=note: print w/replies, xml ) Need Help??


in reply to Looking for suggestions, sorting data from XML or JSON

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-26 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found