in reply to Re^4: How to parse this hash? And how to describe my probem?
in thread How to parse this hash? And how to describe my probem?

A quick and dirty approch with XML::Twig (forget xml::simple) and using xml data in Trace On's scratchpad must be a good starting point for your further investigations: each twig's handler is triggered when the specified XPATH is encountered: notice the ugly cur_league workaround..
#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $cur_league='UNKNOWN'; my $t= XML::Twig->new( twig_handlers => { 'league/id' => sub{ $cur_league = $_[1]->te +xt}, 'event/startDateTime'=>sub{ print "$cur_lea +gue,",$_[1]->text,","; }, 'event/homeTeam/name'=>sub{ print $_[1]->te +xt }, 'event/awayTeam/name'=>sub{ print ",",$_[1] +->text,"\n"; } #add more handlers as needed.. } ); $t->parsefile('trace_on.xml'); __OUTPUT__ 1739,2015-06-24T11:59:00Z,Union Mar del Plata,C. Cordoba Santiago 1739,2015-06-08T20:59:00Z,Douglas Haig,Santamarina Tandil 1740,2015-06-30T16:59:00Z,Arsenal de Sarandi,Crucero del Norte 1740,2015-06-14T11:59:00Z,Aldosivi Mar del Plata,Rosario Central 1740,2015-06-14T11:59:00Z,Arsenal de Sarandi,Gimnasia La Plata 1740,2015-06-14T11:59:00Z,Atletico Huracan,Defensa y Justicia 1740,2015-06-14T11:59:00Z,Belgrano de Cordoba,Argentinos Juniors 1740,2015-06-14T11:59:00Z,Estudiantes La Plata,San Martin San Juan 1740,2015-06-14T11:59:00Z,Godoy Cruz Antonio Tomba,Crucero del Norte 1740,2015-06-14T11:59:00Z,Independiente,Olimpo Bahia Blanca 1740,2015-06-14T11:59:00Z,Lanus,Atletico Rafaela 1740,2015-06-14T11:59:00Z,Newells Old Boys,Racing Club 1740,2015-06-14T11:59:00Z,Nueva Chicago BA,Colon de Santa Fe 1740,2015-06-14T11:59:00Z,Quilmes,Banfield 1740,2015-06-14T11:59:00Z,River Plate,Temperley 1740,2015-06-14T11:59:00Z,Sarmiento,Boca Juniors 1740,2015-06-14T11:59:00Z,Union Santa Fe,San Lorenzo 1740,2015-06-14T11:59:00Z,Velez Sarsfield,Club Atletico Tigre 1740,2015-06-08T20:59:00Z,Club Atletico Tigre,Aldosivi Mar del Plata 1740,2015-06-08T23:09:00Z,Defensa y Justicia,Estudiantes La Plata 1756,2015-06-08T10:29:00Z,Bulleen Lions,Brunswick City


L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^6: How to parse this hash? XML question (Twig example)
by Trace On (Novice) on Jun 09, 2015 at 07:42 UTC

    Thank you! That helped a lot. I spent quite some time to do some basic research on XML Twig and XML.

    But I still do not understand how your workaround works. It is cool that you get the league-number for every line. I would like to have something like that for all the 'spreads' as well. But that seems to involve some kind of recursion. And my little brain is to small for that. I have tried to understand this tutorial. But I am not all the way trough...

    pad://http://www.xml.com/lpt/a/750

        Hm. Ok. I am a step further. But why don't I get the right values with this:

        my $t= XML::Twig->new( twig_handlers => { 'league/id' =>sub +{ $leagueId = $_[1]->text;}, 'event/startDateTime' + =>sub{ $date = $_[1]->text;}, 'event/homeTeam/name' + =>sub{ $home = $_[1]->text;}, 'event/awayTeam/name' + =>sub{ $away = $_[1]->text;}, 'event/periods/period/number' + =>sub{ $period = $_[1]->text;}, 'event/periods/period/description' + =>sub{ $perDescr = $_[1]->text;}, 'event/periods/period/maxBetAmount/moneyLine +' =>sub{ $maxBetML = $_[1]->text;}, 'event/periods/period/moneyLine/homePrice' + =>sub{ $homePrice = $_[1]->text;}, 'event/periods/period/moneyLine/drawPrice' + =>sub{ $drawPrice = $_[1]->text;}, 'event/periods/period/moneyLine/awayPrice' + =>sub{ $awayPrice = $_[1]->text; print "\nMoney,", $lea +gueId, " ", $date, " ", $home, " ", $away, " ", $period, +" ", $perDescr, " ", $maxBetML, " ", $homePrice, " ", $dr +awPrice, " ", $awayPrice; } );

        Your code gives me all fixtures. I am now looking for all moneyline-odds for these fixtures. In the XML there are different periods for a game (mostly match and 1st half). Not all of the periods are given for all matches. And if there is a period there does not have to be a moneyline.

        So I tried the magic workaround with other data. My code tries to store the other relevant data in scalars and then put all together with the last handler-line. But the data is mixed up.

      If you add  warn $_->path, "\n"; to every callback you can observe a magical hint

      Then try something  'spreads/spread' => sub { ... } ... http://xmltwig.org/tutorial/