in reply to Re^6: How to parse this hash? XML question (Twig example)
in thread How to parse this hash? And how to describe my probem?

As side note XML::Twig has his own site with many tutorials and a helpfull QuickReference.

In my homenode i also have this bounch of links about the topics:

XML "If you need to deal with XML, first, we’re very sorry." b d foy http://www.effectiveperlprogramming.com/2011/07/rewrite-xml-with-xmltwig/ and http://www.effectiveperlprogramming.com/2010/03/process-xml-data-with-xmltwig/ and http://it-is-etc.blogspot.it/2012/07/perl-how-to-manipulate-xml-files-using.html and http://perlmeme.org/tutorials/parsing_xml.html speed comparison http://www.robinclarke.net/archives/xml-parsing-with-perl mirod schratchpad and Re: Another simple XML Twig question and http://www.xml.com/pub/a/2001/03/21/xmltwig.html ambrus's Do not reinvent the wheel: real-world example using XML::Twig and also http://perl-xml.sourceforge.net/faq/ and choroba about XML

The trick about the league (that relies on the stability of the XML feeded and that make the code somehow fragile) is very simple:
The $cur_league begins with a dummy value. Then, when the file is parsed (from first line to the bottom) and when a league tag is encountered the relative twig_handler is triggered, setting $cur_league with a real value.
Then are triggered handlers for events (date,team..): when the first date is found the handler prints the league, a comma, the date, another comma. The current league is so valid for all events included in the <league> </league>. Next occurence of 'league' will set $cur_league to a new value. And so on.
i hope i was clear.. if no, let me know


HtH
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^8: How to parse this hash? XML question (Twig example)
by Trace On (Novice) on Jun 09, 2015 at 11:17 UTC

    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.

      Eh no! you need to squeeze yourself a bit more.

      The cur_league tricks works because each event is contained strictly inside a league.

      If the event can contain different items, some of them not required, you need a big handler for the event. In this handler you need to check the presence and the content of required (by you) fields and populate the CSV row accordingly.

      XML::Twig has many (oh really maaany) methods to inspect silb, ancestor, attributes..

      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.