Re: trying to parse out the simple xml file from the book with XML::Pa

2008-11-02 Thread Jeff Pang
I didn't see you assign a value to %forecast but you can loop through it,why?


- Original Message -
From: Richard Lee 
To: Perl Beginners 
Subject: trying to parse out the simple xml file from the book with XML::Parser
 and I get not well-formed error
Date: 2008-11-3 09:48:05

use strict;

use warnings;



use XML::Parser;



my %forecast;

my @curr;

my $type;



my $p1 = new XML::Parser(Style = 'Stream');

my $yahoo = shift;

$p1-parsefile($yahoo);



print Outlook: $forecast{outlook}\n;



foreach (keys %forecast) {

next if /outlook/;

print $_: $forecast{$_}-{val} $forecast{$_}-{deg}\n;

}



---
新浪空间——与朋友开心分享网络新生活!(http://space.sina.com.cn/ )

Re: trying to parse out the simple xml file from the book with XML::Pa

2008-11-02 Thread Richard Lee

Jeff Pang wrote:

I didn't see you assign a value to %forecast but you can loop through it,why?


- Original Message -
From: Richard Lee 
To: Perl Beginners 
Subject: trying to parse out the simple xml file from the book with XML::Parser

 and I get not well-formed error
Date: 2008-11-3 09:48:05

use strict;

use warnings;



use XML::Parser;



my %forecast;

my @curr;

my $type;



my $p1 = new XML::Parser(Style = 'Stream');

my $yahoo = shift;

$p1-parsefile($yahoo);



print Outlook: $forecast{outlook}\n;



foreach (keys %forecast) {

next if /outlook/;

print $_: $forecast{$_}-{val} $forecast{$_}-{deg}\n;

}



---
??(http://space.sina.com.cn/ )
I am not sure if you got the entire code but after the loop through, 
there are more codes which when it encounters tags, it builds %forecast 
hash as below


sub StartTag {
my ($p, $tag) = @_;

push @curr, $tag;

if ($tag eq 'TEMPERATURE') {
$type = $_{TYPE};
$forecast{$type}-{deg} = $_{DEGREES};
}
}

sub EndTag {
pop @curr;
}

sub Text {
my ($p) = shift;

return unless /\S/;

s/^\s+//;
s/^\s+$//;

if ($curr[-1] eq 'OUTLOOK') {
$forecast{outlook} .= $_;
} elsif ( $curr[-1] eq 'TEMPERATURE' ) {
$forecast{$type}-{val} = $_;
}
}

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/