in reply to Parsing XML???
First of all, you'll want to change your XML file since it is not valid: the second line lacks a DTD file name. The fourth and fifth line are not valid either. See the XML 1.0 specs for details. For simplicities sake I changed the document declaration to:
leaving out the other lines.<?xml version="1.0"?>
The following code will parse the file and print the data you wish to extract.
#!perl use strict; use warnings; use XML::Parser; my $parser = new XML::Parser(Handlers => {'Start' => \&startHandler, 'Char' => \&charHandler, 'End' => \&endHandler}); my $mode = undef; $parser->parsefile('data.xml'); sub startHandler { my $self = shift(); my $element = shift(); my %attributes = @_; if ($element eq 'LastTradePrice') { $mode = 'LastTradePrice'; } elsif ($element eq 'NetChange') { $mode = 'NetChange'; } elsif ($element eq 'LastTradeDate') { $mode = 'LastTradeDate'; } } sub charHandler { my $self = shift(); my $str = shift(); if (defined $mode) { print "$mode: $str\n"; } } sub endHandler { my $self = shift(); $mode = undef; }
Hope this helps, -gjb-
|
|---|