in reply to Re: Extracting XML data from a tree
in thread Extracting XML data from a tree
Your code would break if there were multiple <Time> tag in one <Times>. Here's one of the ways to do this with XML::Rules. It handles the multiple <Time> tags and stops processing the XML once it finds the requested day.
use strict; use XML::Rules; use Data::Dumper; my $parser = XML::Rules->new( strispaces => 7, rules => { Time => 'as array', Times => 'pass', 'Day' => sub { my ($tag_name, $attrs, $context, $parent_data, $parser) = +@_; if ($attrs->{'day-of-week'} eq $parser->{parameters}) { $parser->return_this(join(', ', grep( defined($_), map + $_->{'start-time'}, @{$attrs->{Time}}))); } }, Month => sub{} } ); my $time = $parser->parse(\*DATA, 'MondayX'); print "Time: $time\n"; __DATA__ <?xml version="1.0" encoding="utf-8"?> <Month> <Week>
Jenda
Enoch was right!
Enjoy the last years of Rome.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Extracting XML data from a tree
by hdb (Monsignor) on Jun 05, 2013 at 08:51 UTC |
In Section
Seekers of Perl Wisdom